June 13th, 2001, 08:45 AM
Python's interaction with Cookies
Hey all. Not sure how many people read this forum yet, but hopefully I can find someone who knows the answer. In Python, how can I grab what cookies have been already set? I know how to set the cookies, but what if I want to check to see if a cookie already exists that I didn't set with Python? Is there a function for this? The examples given in Python's documentation weren't too specific in retrieving cookies, only in setting them itself.
June 13th, 2001, 03:57 PM
*wipes the sweat from his brow*
Well, after roughly five or so hours of laboring away at this problem of mine, I finally was able to figure out how to get pre-existing cookies from the browser in order to use them. Here's what I finally came up with:
import cgi, os
from Cookie import SimpleCookie
The_Cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
The_Cookie = SimpleCookie()
What this does is if there are pre-existing cookies for that site, it will create a varialbe "The_Cookie" with those variables in it. If there are no pre-existing variables, it will create an empty variable. Hope this helps anyone else who was having this problem.
March 28th, 2005, 08:47 AM
Hey tried the given code,
Originally Posted by GabePreston
I get nothing in my output,
but when i run the python code, i dont get any cookies.
why is tat.
Please help me in this
March 28th, 2005, 09:20 AM
It's because you can't just read cookies as if they were something system-wide. They only make sense in the context of web browsers and web servers, otherwise they are just browser-specific files.
When you browse to a website in a web browser, it will check for stored cookies for that domain, and send them to the web server along with the standard request. On the web server, if using a CGI script, you will get the cookie for that session from that client for that domain available as a local environment variable.
Reading a cookie from os.environ['HTTP_COOKIE'] only makes sense if your script is running as a CGI script as part of a web server...