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

Cookie.py troubles.

I'm confused about the way Cookie.load outputs cookies. Is there a way I
can tell it to give me the value of a key?

I set my cookie like this:

mycookie = Cookie.SimpleCookie()
mycookie['key'] = "foobar"

print mycookie
print("Content-Type: text/html\n\n")
print("meow")

Now, to retrieve it, the documentation says I parse the HTTP_COOKIE
environment variable. Makes sense to me. But when I print it out I get
"Set-Cookie: key=foobar;". I was thinking of parsing it with cgi.parse_qs,
but even if i do print(mycookie.output(header="")) there's still a ; at
the end of the cookie.

Here's what I use to print them out:

cookie = Cookie.SimpleCookie()
cookie.load(os.environ['HTTP_COOKIE'])

print("Content-Type: text/html\n\n")
print(mycookie.output(header=""))

Output:

key=foobar;
Can anyone provide any advice?

- Ben Hearsum
Jul 18 '05 #1
1 3568
Ben Hearsum <bh******@myrealbox.com> writes:
[...]
I set my cookie like this:

mycookie = Cookie.SimpleCookie()
mycookie['key'] = "foobar"

print mycookie
print("Content-Type: text/html\n\n")
print("meow")

Now, to retrieve it, the documentation says I parse the HTTP_COOKIE
environment variable. Makes sense to me. But when I print it out I get
"Set-Cookie: key=foobar;". I was thinking of parsing it with cgi.parse_qs,
but even if i do print(mycookie.output(header="")) there's still a ; at
the end of the cookie.
Why do you want to parse Set-Cookie if you're writing a CGI script?

Set-Cookie is what the server sends to the client. HTTP_COOKIE is set
by the server, not by Python, to the value than was sent by the client
to the server. I guess SimpleCookie has a __str__ method that makes
it print out Set-Cookie: key=foobar; -- seems like useful behaviour.
Here's what I use to print them out:

cookie = Cookie.SimpleCookie()
cookie.load(os.environ['HTTP_COOKIE'])
Yep, in comes the Cookie: header, I presume SimpleCookie parses it,
and then...

print("Content-Type: text/html\n\n")
print(mycookie.output(header=""))
You send the cookie back to the client again.

Output:

key=foobar;


This seems to contradict what you said before ("key=foobar;"
vs. "Set-Cookie: key=foobar;").

John
Jul 18 '05 #2

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

Similar topics

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...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
7
by: ehendrikd | last post by:
hi all i need some clarification on how the php session work in relation to cookies. we have a web site where users need to log in. a few of our users were having troubles with their browser...
1
by: Owen | last post by:
I have a web app that is a mixture of ASP and ASP.NET pages. Largely the only data passed between them is via the querystring, or by reading from a database. However there is a requirement for...
0
by: csgraham74 | last post by:
Hi, Im trying to make changes to an appliction that i inherited. i have set several cookies with different small values from XML. i get to a piece of code where i response.redirect to another...
2
by: RootShell | last post by:
I have been experiencing a lot of troubles in reading a PHP COOKIE from javascript. if i write the cookie and read it in javascript in the same PHP file it works well, but if i write the cookie...
4
by: gcervantes | last post by:
Hello, I am making a screen scraper, in other programs I have been able to log into a site, get the cookie, submit a form and read the results. However, this time I am in troubles, the site I need...
9
by: ziycon | last post by:
I am creating a cookie when a user clicks on a link and saving a value based on the link clicked, everytime the user goes to a different page i want the JS to check if the cookie already exists and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.