473,385 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2087
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: N.K | last post by:
Hi , Python's existing cookie module doesnt supports new cookie headers SetCookie2 , How to submit a patch for that ? I tried emailing person who owns that module.But no response. Thanks,...
2
by: sh | last post by:
Hi guys, Well, I have a (maybe dumb) question. I want to write my own little blog using Python (as a fairly small but doable project for myself to learn more deaply Python in a web context). ...
0
by: | last post by:
I''m having a problem with cookies that is driving me insane :). - If a user comes to http://domain.com and a cookie is set for them, then the user for whatever reason jumps to http://www.domain.com...
2
by: Mike | last post by:
1. For some reason after the session has ended and the authentication cookie has expired I'm not being redirected to the login page. Insted I'm be assigned a new authentication cookie? Anyone have...
6
by: thomson | last post by:
Hi All, i do hae a solution in which i do have mulitple projects including Web Projects,, Depending on the functionality it gets redirected to different web projects and it is working fine, ...
6
by: Larry Rebich | last post by:
How do I read and write a cookie in an ASP.Net module? I can get this code to work in an aspx.vb class but not in a regular VB module: Response.Cookies.Add(c) Response is not a recognized...
14
by: ccdetail | last post by:
http://www.tiobe.com/index.htm?tiobe_index Python is the 7th most commonly used language, up from 8th. The only one gaining ground besides VB in the top 10. We're glad, our app is written in...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
4
by: rodmc | last post by:
Hi, I am trying to set a cookie on a client computer using the Cookie module however all I get is the text being printed in the browser window. Can anyone point me in the right direction so that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.