470,613 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

problems with module Cookie

Hi.
I'm using the Cookie module (on the client side).
I have found a problem trying to parse the cookie:

"Set-Cookie: value=thevalue; path=/;
expires=Fri, 21-May-2004 10:40:51 GMT"

The date is not parsed correctly, only "Fri," is matched.
Thanks and regards Manlio Perillo
Jul 18 '05 #1
13 1976
Manlio Perillo <NO******************@libero.it> pisze:
I'm using the Cookie module (on the client side).
I have found a problem trying to parse the cookie:

"Set-Cookie: value=thevalue; path=/;
expires=Fri, 21-May-2004 10:40:51 GMT"


You must use date in UTC format.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #2
On Sat, 22 May 2004 20:06:45 +0000 (UTC), Jarek Zgoda
<jz****@gazeta.usun.pl> wrote:
Manlio Perillo <NO******************@libero.it> pisze:
I'm using the Cookie module (on the client side).
^^^ I have found a problem trying to parse the cookie:

"Set-Cookie: value=thevalue; path=/;
expires=Fri, 21-May-2004 10:40:51 GMT"


You must use date in UTC format.


This is the date format used by the server cookie!


Thanks and Regards Manlio Perillo
Jul 18 '05 #3
Manlio Perillo <NO******************@libero.it> pisze:
I'm using the Cookie module (on the client side).
^^^ I have found a problem trying to parse the cookie:

"Set-Cookie: value=thevalue; path=/;
expires=Fri, 21-May-2004 10:40:51 GMT"


You must use date in UTC format.


This is the date format used by the server cookie!


See http://www.w3.org/TR/NOTE-datetime on how should date look in
cookies. If date is not in valid format, the module functions may have
trouble decoding it.

--
Jarek Zgoda
http://jpa.berlios.de/
Jul 18 '05 #4
On Sun, 23 May 2004 18:36:52 GMT, JanC <us*********@janc.invalid>
wrote:
Manlio Perillo <NO******************@libero.it> schreef:
I'm using the Cookie module (on the client side).


Do you know the ClientCookie module?
<http://wwwsearch.sourceforge.net/ClientCookie/>


Yes, I know; but the standard Cookie module is sufficient.

Thanks and regards
Jul 18 '05 #5
On Sun, 23 May 2004 17:12:25 +0000 (UTC), Jarek Zgoda
<jz****@gazeta.usun.pl> wrote:
Manlio Perillo <NO******************@libero.it> pisze:
>You must use date in UTC format.
This is the date format used by the server cookie!
See http://www.w3.org/TR/NOTE-datetime on how should date look in
cookies. If date is not in valid format, the module functions may have
trouble decoding it.
It is not a my problem!


As you see, it's your problem, since it's you who cann't decode this
cookie. ;)


The problem is also of Cookie module.
The web server follow the Netscape specification for Cookies and in
this spec the date is in the format I have posted.


Netscape is not internet God, W3C is.


Unfortunately we don't live in a perfect world... ;)
I never tried to read cookies using Cookie module (I used Python only
for writing), so I cann't help more here. Good luck.


Ok, thanks.

Regards Manlio Perillo
Jul 18 '05 #6
Manlio Perillo <NO******************@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 ;-)

The Cookie module really doesn't know how to handle cookies on the
client side. Use this, which does:

http://wwwsearch.sf.net/ClientCookie/
You can just say:

ClientCookie.urlopen("http://www.example.com/")

and be done with it.
John
Jul 18 '05 #7
On 27 May 2004 22:52:10 +0100, jj*@pobox.com (John J. Lee) wrote:
Manlio Perillo <NO******************@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|D ec)"

_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
Jul 18 '05 #8
Manlio Perillo <NO******************@libero.it> writes:
On 27 May 2004 22:52:10 +0100, jj*@pobox.com (John J. Lee) wrote: [...]
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.


Having written it, I agree, but I don't think it's my fault <wink>.

Standard Cookie module (with my corrections) plus httplib module is
really all I need.


Cool.
John
Jul 18 '05 #9
On 29 May 2004 12:26:59 +0100, jj*@pobox.com (John J. Lee) wrote:
Manlio Perillo <NO******************@libero.it> writes:
On 27 May 2004 22:52:10 +0100, jj*@pobox.com (John J. Lee) wrote:[...]
>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.


Having written it, I agree, but I don't think it's my fault <wink>.


Well, it is too complicated for me.
Standard Cookie module (with my corrections) plus httplib module is
really all I need.


Cool.


Yes, there exists programs that are simple (as there exist programming
languages that are simple)!

Actually what I do is to download several files from a server.
Some files/pages are generated by a script (so I have to post an
x-www-form-urlencoded string).

I don't want to use urllib2 because it (as I think) for every request
connects to the server, do the request and disconnect.

I need cookies because the server (as many other) authenticate user
with cookies.
So the simple algorithm is:

-connect to the server
-read a cookie from a file
-send the cookie to the server
-if the server send a cookie, the old one must be updated: with
standard Cookie this is simple: cookie.update(newcookie)
- ...
- save the cookie to a file
This is very simple to do with httplib and Cookie modules, so why to
use more involved modules?

Regards Manlio Perillo
Jul 18 '05 #10
Manlio Perillo <NO******************@libero.it> writes:
[...]
Standard Cookie module (with my corrections) plus httplib module is
really all I need.
Cool.


Yes, there exists programs that are simple (as there exist programming
languages that are simple)!

Actually what I do is to download several files from a server.
Some files/pages are generated by a script (so I have to post an
x-www-form-urlencoded string).

I don't want to use urllib2 because it (as I think) for every request
connects to the server, do the request and disconnect.


Yes, that's true. I should fix it...

I need cookies because the server (as many other) authenticate user
with cookies.
So the simple algorithm is:

-connect to the server
-read a cookie from a file
-send the cookie to the server
-if the server send a cookie, the old one must be updated: with
standard Cookie this is simple: cookie.update(newcookie)
- ...
- save the cookie to a file
This is very simple to do with httplib and Cookie modules, so why to
use more involved modules?


No reason at all if you're happy with it, of course. That was what my
"Cool" was meant to communicate.

*Using* urllib2 is less involved even than your little algorithm
above, of course (neglecting bugs, of course, including the persistent
connection bug, which -- though it could certainly be a problem -- I
confess has never actually troubled me, despite having used it to
repeatedly fetch tens of millions of records in the past).

Why use modules whose *implementation* is more involved? Because
they're even easier to use, and because, even for simple scripts like
your's, not every case is as simple as the one you happen to have met.
I know from experience that it can quickly get *very* tiresome to do
some of this stuff by hand (even if only a few things like
redirections and cookies are involved). And personally, I don't even
like to *think* about it, or to have to maintain it in future if I can
avoid it, regardless of how simple (ignoring for the moment that I
have to maintain the library itself ;-).

As for why the complications... well, if you want to know, read the
code (no, don't :-).
John
Jul 18 '05 #11
On 31 May 2004 01:56:10 +0100, jj*@pobox.com (John J. Lee) wrote:
I need cookies because the server (as many other) authenticate user
with cookies.
So the simple algorithm is:

-connect to the server
-read a cookie from a file
-send the cookie to the server
-if the server send a cookie, the old one must be updated: with
standard Cookie this is simple: cookie.update(newcookie)
- ...
- save the cookie to a file
This is very simple to do with httplib and Cookie modules, so why to
use more involved modules?
No reason at all if you're happy with it, of course. That was what my
"Cool" was meant to communicate.

*Using* urllib2 is less involved even than your little algorithm
above, of course (neglecting bugs, of course, including the persistent
connection bug, which -- though it could certainly be a problem -- I
confess has never actually troubled me, despite having used it to
repeatedly fetch tens of millions of records in the past).

Why use modules whose *implementation* is more involved? Because
they're even easier to use, and because, even for simple scripts like
your's, not every case is as simple as the one you happen to have met.


You are right, but: what means 'easier to use'?
There is less code to write?
There is less theory to learn?

For my script using urllib2 does not make the program easier.
I know from experience that it can quickly get *very* tiresome to do
some of this stuff by hand (even if only a few things like
redirections and cookies are involved).


In my case cookie are very easy to use and I simply ignore
redirection...

Of course I agree with you for all other cases, but there exist
programs that really needs only low level library.

Actually, ad example, standard Cookie module is low level.
It only parses key=value pairs, and, more important, it is 'other
library' neutral.
That is, BaseCookie class has a parse method for parsing a string
(as SetCookie: key=value; ....), and an OutputString (added by me)
that returns the cookie data.

Thanks and regards Manlio Perillo

Jul 18 '05 #12
Manlio Perillo <NO******************@libero.it> writes:
On 31 May 2004 01:56:10 +0100, jj*@pobox.com (John J. Lee) wrote: [...]
This is very simple to do with httplib and Cookie modules, so why to
use more involved modules?


No reason at all if you're happy with it, of course. That was what my
"Cool" was meant to communicate.

[...] Of course I agree with you for all other cases, but there exist
programs that really needs only low level library.
Was that not what I said? Sorry if I'm not making myself clear!
(What follows is unrelated to your (quite unnecesary!) extended
defence of your use of Cookie in your script, but just by the way of
commentary on the points you make)
Actually, ad example, standard Cookie module is low level.
Yes. The low-level stuff it does is not not always the right thing
for client-side code, though.

It only parses key=value pairs, and, more important, it is 'other
library' neutral.

[...]

Same goes for ClientCookie. The interface required of request and
response objects is defined in the docs. For doing what ClientCookie
does (automatic cookie handling), I don't think it can get much
simpler.
John
Jul 18 '05 #13
On 01 Jun 2004 20:28:03 +0100, jj*@pobox.com (John J. Lee) wrote:
Manlio Perillo <NO******************@libero.it> writes:
On 31 May 2004 01:56:10 +0100, jj*@pobox.com (John J. Lee) wrote:[...]
>> This is very simple to do with httplib and Cookie modules, so why to
>> use more involved modules?
>
>No reason at all if you're happy with it, of course. That was what my
>"Cool" was meant to communicate.

[...]
Of course I agree with you for all other cases, but there exist
programs that really needs only low level library.


Was that not what I said? Sorry if I'm not making myself clear!


I'm sorry but I not a very expert in english language...

(What follows is unrelated to your (quite unnecesary!) extended
defence of your use of Cookie in your script, but just by the way of
commentary on the points you make)
Actually, ad example, standard Cookie module is low level.
Yes. The low-level stuff it does is not not always the right thing
for client-side code, though.


Why?
It only parses key=value pairs, and, more important, it is 'other
library' neutral.[...]

Same goes for ClientCookie. The interface required of request and
response objects is defined in the docs.


Ok, but I don't want to write additional code for implementing request
and response interface...
For doing what ClientCookie
does (automatic cookie handling), I don't think it can get much
simpler.


Ok. But my program is already very simple using Cookie.

Thanks and regards Manlio Perillo

Jul 18 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by N.K | last post: by
2 posts views Thread by sh | last post: by
reply views Thread by | last post: by
2 posts views Thread by Mike | last post: by
6 posts views Thread by thomson | last post: by
6 posts views Thread by Larry Rebich | last post: by
14 posts views Thread by ccdetail | last post: by
4 posts views Thread by rodmc | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.