473,725 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SimpleCo okie()
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 3161
On Apr 28, 9:42 am, cbh...@gmail.co m 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.SimpleCo okie()
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(coo kie))

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(co okie=None, environ=None):
if cookie is None:
if environ is None:
environ = os.environ
cookie = environ.get('HT TP_COOKIE', '')
if cookie == '':
return {}
c = SimpleCookie()
c.load(cookie)
cookiedict = {}
for key in c.keys():
cookiedict[key] = c.get(key).valu e
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.co m 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.SimpleCo okie()
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(coo kie))

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(co okie=None, environ=None):
if cookie is None:
if environ is None:
environ = os.environ
cookie = environ.get('HT TP_COOKIE', '')
if cookie == '':
return {}
c = SimpleCookie()
c.load(cookie)
cookiedict = {}
for key in c.keys():
cookiedict[key] = c.get(key).valu e
return cookiedict

===
All the best. -- Aaron Watters

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

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=def ault+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.co m 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=def ault+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
2076
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: ---------------------------------------------------------------------------- strLogon= Request.ServerVariables("LOGON_USER") Set RS = Server.CreateObject("ADODB.Recordset") strSQL = "SELECT EmployeeID, DepartmentID " strSQL = strSQL & "FROM Employee " strSQL = strSQL & "WHERE NetworkID =...
3
1266
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 object is actually just setting a header, of course. Every redirect is sending a header to the browser equivalent to a "meta refresh" and that once again triggers the session_onstart. Geezus. Duh.
6
12938
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 here put me straight on the following: I had a look at the time() function came across this: "To clarify, it seems this function returns the time of the computer's clock and does not do any timezone adjustments to return GMT, so you are...
5
1246
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 remember certain data to cookies. At some point in my application the following code is executed
5
3314
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 document.cookie variable combine all cookie key=value pairs? All of the examples I've seen discuss referencing a specific cookie. I don't see how this is done. Cookies are usually named by the domain. If I want to reference a specific cookie, do I...
17
7236
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 and in the debugger I can see the value I want in the property
2
2608
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 same URL directly it will display as logged in page. regards
2
1876
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 encrypted ticket to it. Both of these have an expiration date, but I'm not seeing how to use them. Does it make sense that these dates would ever differ? Thanks,
5
1690
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
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.