Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting and Setting Cookies

Reply
Thread Tools

Getting and Setting Cookies

 
 
Vlad Dogaru
Guest
Posts: n/a
 
      07-18-2006
Hello,

I am trying to use cookies and Python to create a simple login example.
But I am very disoriented at the existence of two cookie libraries,
namely Cookie and cookielib. I have seen examples of setting cookies
(although I am still not sure about timestamps and cookie lifespan),
but no reference to getting the currently set cookies. For instance, I
want to see if there is any 'user' value, to check whether the user has
logged in. Please, enlighten me.

Thanks in advance,
Vlad

 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      07-18-2006
"Vlad Dogaru" <(E-Mail Removed)> writes:

> I am trying to use cookies and Python to create a simple login example.
> But I am very disoriented at the existence of two cookie libraries,
> namely Cookie and cookielib. I have seen examples of setting cookies

[...]

From the cookielib docs:

http://docs.python.org/lib/module-cookielib.html

| The cookielib module defines classes for automatic handling of HTTP
| cookies. It is useful for accessing web sites that require small
| pieces of data - cookies - to be set on the client machine by an HTTP
| response from a web server, and then returned to the server in later
| HTTP requests.

(note the *accessing* there)

[...]

| Module Cookie: HTTP cookie classes, principally useful for server-side
| code. The cookielib and Cookie modules do not depend on each
| other.


Module cookielib is for web client code (writing code that works like
a browser). Module Cookie is for server-side code (writing code to
make a web site work). You don't make it entirely clear which you're
doing, but it sounds like the latter.


John
 
Reply With Quote
 
 
 
 
Vlad Dogaru
Guest
Posts: n/a
 
      07-19-2006

John J. Lee wrote:
> "Vlad Dogaru" <(E-Mail Removed)> writes:
>
> > I am trying to use cookies and Python to create a simple login example.
> > But I am very disoriented at the existence of two cookie libraries,
> > namely Cookie and cookielib. I have seen examples of setting cookies

> [...]
>
> From the cookielib docs:
>
> http://docs.python.org/lib/module-cookielib.html
>
> | The cookielib module defines classes for automatic handling of HTTP
> | cookies. It is useful for accessing web sites that require small
> | pieces of data - cookies - to be set on the client machine by an HTTP
> | response from a web server, and then returned to the server in later
> | HTTP requests.
>
> (note the *accessing* there)
>
> [...]
>
> | Module Cookie: HTTP cookie classes, principally useful for server-side
> | code. The cookielib and Cookie modules do not depend on each
> | other.
>
>
> Module cookielib is for web client code (writing code that works like
> a browser). Module Cookie is for server-side code (writing code to
> make a web site work). You don't make it entirely clear which you're
> doing, but it sounds like the latter.


I am trying to write a simple login script. I understand (or rather I
think I understand) how to set a cookie with the Cookie module. My
problem is getting the cookies that are currently set. How can I do
that?

 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      07-19-2006
"Vlad Dogaru" <(E-Mail Removed)> writes:
[...]
> I am trying to write a simple login script. I understand (or rather I
> think I understand) how to set a cookie with the Cookie module. My
> problem is getting the cookies that are currently set. How can I do
> that?


You still haven't explicitly said that you're writing server-side
code. If you are:

IIRC, you .load() it from the HTTP header value, then you use it as a
mapping (though, oddly, that seems undocumented, except in the
"Example" section of the docs). A working CGI-based example (written
in a rather retro style for ease of deployment):

http://codespeak.net/svn/wwwsearch/C...cookietest.cgi


If you want to iterate over all cookies using module Cookie (that
script does not), use .keys(), .values() or .items() on your
SimpleCookie instance (it's also a little odd that instances of class
SimpleCookie do not represent a single cookie, but a collection of
cookies).


John
 
Reply With Quote
 
Vlad Dogaru
Guest
Posts: n/a
 
      07-20-2006
John J. Lee wrote:
> "Vlad Dogaru" <(E-Mail Removed)> writes:
> [...]
> > I am trying to write a simple login script. I understand (or rather I
> > think I understand) how to set a cookie with the Cookie module. My
> > problem is getting the cookies that are currently set. How can I do
> > that?

>
> You still haven't explicitly said that you're writing server-side
> code. If you are:
>
> IIRC, you .load() it from the HTTP header value, then you use it as a
> mapping (though, oddly, that seems undocumented, except in the
> "Example" section of the docs). A working CGI-based example (written
> in a rather retro style for ease of deployment):
>
> http://codespeak.net/svn/wwwsearch/C...cookietest.cgi
>
>
> If you want to iterate over all cookies using module Cookie (that
> script does not), use .keys(), .values() or .items() on your
> SimpleCookie instance (it's also a little odd that instances of class
> SimpleCookie do not represent a single cookie, but a collection of
> cookies).


That's pretty much what I was trying to find out. Thanks for the
pointer.

Vlad

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Give Request.Cookies and Response.Cookies is there any reason to use another method to use cookies? _Who ASP .Net 7 09-18-2008 07:49 PM
Persistant cookies and non-persistant cookies. archana ASP .Net 1 05-25-2007 08:43 AM
Response.Cookies vs Request.Cookies Alex Nitulescu ASP .Net 1 02-03-2005 09:43 AM
Anyone have code for setting/getting cookies with cookie/key/value format? Mike Javascript 1 09-04-2004 01:56 PM
Issue with Cookies not getting deleted even after setting a previous date in expires property techie ASP General 3 09-17-2003 09:56 PM



Advertisments