Thread: Python cookies

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0

    Unhappy Python cookies


    Hey all,

    I am trying to read the cookie set by javascript using python

    I used the basic cookie function to set cookies using javascript

    I got error while reading it using python

    Help me out

    My code : for python cookies


    #!C:/Python23/python.exe

    from Cookie import *
    from os import *
    from cgi import *

    c=Cookie()
    c.load(environ['HTTP_COOKIE'])
    print c

    My error


    C:\Python23\lib\Cookie.py:712: DeprecationWarning: Cookie/SmartCookie class is insecure; do not use it
    DeprecationWarning)

    Traceback (most recent call last):
    File "C:\Documents and Settings\Arpan Shah\Desktop\dem.py", line 8, in -toplevel-
    c.load(environ['HTTP_COOKIE'])
    File "C:\Python23\lib\os.py", line 417, in __getitem__
    return self.data[key.upper()]
    KeyError: 'HTTP_COOKIE'
    >>>

    Please let me know , why i am getting this error,

    I dont have any environment variable set as HTTP_Cookie

    i am juss running a sample python code to read all cookie data.

    Any help will be appreicated.

    thanz

    Shah
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    1
    Rep Power
    0
    File "C:\Documents and Settings\Arpan Shah\Desktop\dem.py", line 8, in -toplevel-
    c.load(environ['HTTP_COOKIE'])
    File "C:\Python23\lib\os.py", line 417, in __getitem__
    return self.data[key.upper()]
    KeyError: 'HTTP_COOKIE'
    >>>

    Please let me know , why i am getting this error,

    I dont have any environment variable set as HTTP_Cookie
    Exactly as you said, you don't have the environment variable set.

    You probably want this code to read something like:
    if environ.has_key('HTTP_COOKIE'):
    c.load(environ['HTTP_COOKIE'])
    else:
    do whatever...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Some debugging hints:
    • check if the cookie is set in your browser
    • if not, check the cookie policy set in your browser. Be shure to accecpt cookies from this host.
    • If the cookie doesn't get set now, check if javascript is enabled (sounds trivial, just to be shure)
    • if javascript is enabled, check the page source. Be shure that the javascript code is executed.

    The javascript code within the page should look like:
    Code:
    <HTML>
    ...
    ..
    <SCRIPT LANGUAGE="JavaScript">
    <!-- begin hiding
    document.cookie = "hello=world;"
    // end hiding -->
    </script>
    ...
    ...
    </HTML>
    If you have the cookie set in your browser and you still don't get the cookie, there may be a problem with your web server.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The question should be resolved already, but I have to have my say .

    As Orr1and sbkwi have said, the problem has to do with the HTTP_COOKIE header not being available, so it can't be parsed. This is probably because of the way you're running it. It looks to me as if the program is either being run either from the command line or in the interpreter?

    You might also be interested in the cookies.py file in my Net module. Though it doesn't actually use the Cookie module I should give you a better understanding of whats going on .

    http://forums.devshed.com/showthread.php?t=129666

    Take care all,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo