Getting and Setting Cookies

V

Vlad Dogaru

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
 
J

John J. Lee

Vlad Dogaru said:
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
 
V

Vlad Dogaru

John said:
Vlad Dogaru said:
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?
 
J

John J. Lee

Vlad Dogaru said:
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/ClientCookie/trunk/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
 
V

Vlad Dogaru

John said:
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/ClientCookie/trunk/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top