problems with module Cookie

Manlio Perillo NOmanlio_perilloSPAM at libero.it
Sat May 29 02:33:44 EDT 2004


On 27 May 2004 22:52:10 +0100, jjl at pobox.com (John J. Lee) wrote:

>Manlio Perillo <NOmanlio_perilloSPAM at libero.it> writes:
>[...]
>> I'm trying to fix the regular expression patternin Cookie.py but it
>> does not work:
>[...]
>
>Yeah, IIRC there's some odd stuff in there, that doesn't even seem to
>come from the standards, let alone reality ;-)
>

I have fixed the pattern.
For matching spaces it is needed '\ ' and not ' '.

Here is the code.
Now the Cookie parse the Netscape format.


_LegalCharsPatt  = r"[\w\d!#%&'~_`><@,:/\$\*\+\-\.\^\|\)\(\?\}\{\=]"

_WeekPatt = r"(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)"
_MonthPatt = r"(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"

_DatePatt = r"(?:" + _WeekPatt + r",\ \d{2}-" + _MonthPatt +\
            r"-\d{4}\ \d{2}:\d{2}:\d{2}\ GMT)"

_CookiePattern = re.compile(
    r"(?x)"                       # This is a Verbose pattern
    r"(?P<key>"                   # Start of group 'key'
    ""+ _LegalCharsPatt +"+?"     # Any word of at least one letter,\
                                    nongreedy
    r")"                          # End of group 'key'
    r"\s*=\s*"                    # Equal Sign
    r"(?P<val>"                   # Start of group 'val'
    r'"(?:[^\\"]|\\.)*"'            # Any doublequoted string
    r"|"                            # or
    ""+ _DatePatt + ""              # A date as specified by Netscape\
                                      spec
    r"|"                            # or
    ""+ _LegalCharsPatt +"*"        # Any word or empty string
    r")"                          # End of group 'val'
    r"\s*;?"                      # Probably ending in a semi-colon
    )



I also have added a method to BaseCookie that behaves like
Morsel.OutputString:

def OutputString(self, attrs=None, sep='\n'):
        """Return a string suitable for HTTP.
        """
        result = []
        items = self.items()
        items.sort()
        for K,V in items:
            result.append( V.OutputString(attrs) )
        return sep.join(result)



Now the Cookie is usable on the client side too.

>The Cookie module really doesn't know how to handle cookies on the
>client side.  


It does not matter, all the cookie logic for my program is very very
simple and standard Cookie is all I need.

> Use this, which does:
>
>http://wwwsearch.sf.net/ClientCookie/
>
>
>You can just say:
>
>ClientCookie.urlopen("http://www.example.com/")
>
>and be done with it.
>

I have seen the module, but it is too complicated.
Standard Cookie module (with my corrections) plus httplib module is
really all I need.



Thanks and regards   Manlio Perillo



More information about the Python-list mailing list