#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    York, PA
    Posts
    0
    Rep Power
    0

    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.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    York, PA
    Posts
    0
    Rep Power
    0

    Talking *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

    if os.environ.has_key('HTTP_COOKIE'):
    The_Cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
    else:
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by GabePreston
    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

    if os.environ.has_key('HTTP_COOKIE'):
    The_Cookie = SimpleCookie(os.environ['HTTP_COOKIE'])
    else:
    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.
    Hey tried the given code,

    I get nothing in my output,

    But i have used forms - and javascript to set my form data into cookie.

    but when i run the python code, i dont get any cookies.

    why is tat.

    Please help me in this
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    but when i run the python code, i dont get any cookies.

    why is tat.
    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...

IMN logo majestic logo threadwatch logo seochat tools logo