473,320 Members | 2,006 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,320 software developers and data experts.

Cookie Confusion - How to Set a Cookie

Hi -

I am trying my hand at python cookies. I'm confused about a few
things though. Do the python cookies get written to a cookies text
file? I have simple code below -- I see the cookie in my HTTP header
but do not get anything in the cookie text file. I'm working on
linux.

print "Content-type: text/html"
cookie = Cookie.SimpleCookie()
cookie['Test'] = 'abc'
print cookie
print

Are there rules about where in the header the set cookie line should
be?

Thanks in advance!
Christian
Jun 27 '08 #1
5 3140
On Apr 28, 9:42 am, cbh...@gmail.com wrote:
....I see the cookie in my HTTP header
but do not get anything in the cookie text file. I'm working on
linux.

print "Content-type: text/html"
cookie = Cookie.SimpleCookie()
cookie['Test'] = 'abc'
print cookie
print

Are there rules about where in the header the set cookie line should
be?
Hi Christian. I think the cookie can go anywhere in
the header, but I usually put it before the content-type.
If you want to store the cookie to a file,
or even better, to a database of some sort, you have to
do it yourself, the Cookie module doesn't do it for you,
I hope.

# store cookie to /tmp/cookie.txt
file("/tmp/cookie.txt","w").write(str(cookie))

For parsing cookies, I stole and modified this from
the Django source (for use in a cgi script):

===
from Cookie import SimpleCookie
import os

# stolen and modified from Django
def parse_cookie(cookie=None, environ=None):
if cookie is None:
if environ is None:
environ = os.environ
cookie = environ.get('HTTP_COOKIE', '')
if cookie == '':
return {}
c = SimpleCookie()
c.load(cookie)
cookiedict = {}
for key in c.keys():
cookiedict[key] = c.get(key).value
return cookiedict

===
All the best. -- Aaron Watters

===
http://www.xfeedme.com/nucular/pydis...EETEXT=monster
Jun 27 '08 #2
On Apr 28, 1:37 pm, Aaron Watters <aaron.watt...@gmail.comwrote:
On Apr 28, 9:42 am, cbh...@gmail.com wrote:
....I see the cookie in my HTTP header
but do not get anything in the cookie text file. I'm working on
linux.
print "Content-type: text/html"
cookie = Cookie.SimpleCookie()
cookie['Test'] = 'abc'
print cookie
print
Are there rules about where in the header the set cookie line should
be?

Hi Christian. I think the cookie can go anywhere in
the header, but I usually put it before the content-type.
If you want to store the cookie to a file,
or even better, to a database of some sort, you have to
do it yourself, the Cookie module doesn't do it for you,
I hope.

# store cookie to /tmp/cookie.txt
file("/tmp/cookie.txt","w").write(str(cookie))

For parsing cookies, I stole and modified this from
the Django source (for use in a cgi script):

===
from Cookie import SimpleCookie
import os

# stolen and modified from Django
def parse_cookie(cookie=None, environ=None):
if cookie is None:
if environ is None:
environ = os.environ
cookie = environ.get('HTTP_COOKIE', '')
if cookie == '':
return {}
c = SimpleCookie()
c.load(cookie)
cookiedict = {}
for key in c.keys():
cookiedict[key] = c.get(key).value
return cookiedict

===
All the best. -- Aaron Watters

===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=monster

Thanks for the code, Aaron. I will give it a try.

I've been reading some more about cookielib and am not sure whether I
should use Cookie or cookielib. This is what I want to do: a user is
going to login. Upon a successful login, I want to write their name
and date/time of visit to a cookie file. Which is the correct python
module to use?
Jun 27 '08 #3
Thanks for the code, Aaron. I will give it a try.

I've been reading some more about cookielib and am not sure whether I
should use Cookie or cookielib. This is what I want to do: a user is
going to login. Upon a successful login, I want to write their name
and date/time of visit to a cookie file. Which is the correct python
module to use?
Cookie does parsing and generation of cookie strings
for server-side applications like your CGI script.

The cookielib module
is designed for either implementing a client like a web browser
or emulating a client/browser (for web scraping, for example).

I think you want to use Cookie.
The distinction could be made clearer in
the docs, imho.

Also, when you say "write the cookie file" I think you mean
"store the cookie to the client browser". This should happen
automatically when you send the cookie header to the client
correctly (if the client is configured to cooperate).

-- Aaron Watters

===
http://www.xfeedme.com/nucular/pydis...t+does+nothing
Jun 27 '08 #4
On Apr 29, 3:35 pm, Aaron Watters <aaron.watt...@gmail.comwrote:
Thanks for the code, Aaron. I will give it a try.
I've been reading some more about cookielib and am not sure whether I
should use Cookie or cookielib. This is what I want to do: a user is
going to login. Upon a successful login, I want to write their name
and date/time of visit to a cookie file. Which is the correct python
module to use?

Cookie does parsing and generation of cookie strings
for server-side applications like your CGI script.

The cookielib module
is designed for either implementing a client like a web browser
or emulating a client/browser (for web scraping, for example).

I think you want to use Cookie.
The distinction could be made clearer in
the docs, imho.

Also, when you say "write the cookie file" I think you mean
"store the cookie to the client browser". This should happen
automatically when you send the cookie header to the client
correctly (if the client is configured to cooperate).

-- Aaron Watters

===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=default+does+n...

Sorry for the slow replies. I've been in & out with a sick child.

I'm used to my javascript cookies. They are automatically written to
a cookie.txt file in a .mozilla dir under my user. When I say to
'write the cookie file' this is what I was referring to. I was
expecting my python cookie to automatically get written to the same
file. I have't seen this happen yet.
Jun 27 '08 #5
On May 1, 9:02 am, cbh...@gmail.com wrote:
On Apr 29, 3:35 pm, Aaron Watters <aaron.watt...@gmail.comwrote:
Thanks for the code, Aaron. I will give it a try.
I've been reading some more about cookielib and am not sure whether I
should use Cookie or cookielib. This is what I want to do: a user is
going to login. Upon a successful login, I want to write their name
and date/time of visit to a cookie file. Which is the correct python
module to use?
Cookie does parsing and generation of cookie strings
for server-side applications like your CGI script.
The cookielib module
is designed for either implementing a client like a web browser
or emulating a client/browser (for web scraping, for example).
I think you want to use Cookie.
The distinction could be made clearer in
the docs, imho.
Also, when you say "write the cookie file" I think you mean
"store the cookie to the client browser". This should happen
automatically when you send the cookie header to the client
correctly (if the client is configured to cooperate).
-- Aaron Watters
===http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=default+does+n...

Sorry for the slow replies. I've been in & out with a sick child.

I'm used to my javascript cookies. They are automatically written to
a cookie.txt file in a .mozilla dir under my user. When I say to
'write the cookie file' this is what I was referring to. I was
expecting my python cookie to automatically get written to the same
file. I have't seen this happen yet.
Hmmm. I don't know how cookies are stored on the client.
I recommend using the standard cgi debug
environment dump after setting
a cookie to see if the cookie is coming back to the server.
If it is not something is wrong -- you are setting it incorrectly,
or the server or client is blocking the cookie.

Note if you are testing on one machine: some browsers
and servers have by default
special security restrictions on
"localhost" loopbacks -- it may be that this is causing
either the server or the client to ignore the cookie.

-- Aaron Watters
===
http://www.xfeedme.com/nucular/pydis...REETEXT=cooked
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: middletree | last post by:
I am doing an Intranet-based app. For one set of pages on the app, I want to grab the user's network logon ID. I have chosen to do this with this code:...
3
by: ou812 | last post by:
Yes. The session_onstart event will fire for every pull BECAUSE no SESSIONID cookie is ever created and the server has to assume that every HTTP REQUEST frame is a new session. The response...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
5
by: Charly Walter | last post by:
Hello, Please help. I keep data in several session variables, one of which is a complex object tied to a custom DLL I wrote. Everything seems to be running fine until I introduced a system to...
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
17
by: James Johnson | last post by:
Dear C#dex, I define a variable: HttpWebRequest webRequest and run the following request webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest; The webRequest object returns values...
2
by: venkat | last post by:
hi If you close the browser directly then the cookie won't get deleted. When ever you log off only it will get deleted. If you close the browser directly and again the open the browser for the...
2
by: Bill Borg | last post by:
Hello all, I am working on forms authentication and trying to understand: what's the relationship between the cookie expiration and the ticket expiration? I create a cookie and I add an...
5
by: sophie_newbie | last post by:
Does anyone know how to do this? I can't seem to make it work. I'm using: c = Cookie.SimpleCookie() c = "unamepwordwhatever" c.expires = time.time() + 300 print c
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.