How to Read Cookies With Python
Python lets you read user cookies, so you can import user preferences and browser settings stored in the small cookie file. You can only read cookies from the files you created from your own applications unless the Python script runs on the local computer. You retrieve the cookie by name, so you must also know the name of the cookie you want to read when loading the Python settings.
Instructions
-
-
1
Right-click the file you want to use to read the cookies and click "Open With." Click your Python editor program to load the file code.
-
2
Retrieve the cookie and set the value to a Python variable. The following code retrieves a cookie named "mycookie" and assigns the value to "cookievaue":
cookievalue = Cookie.SimpleCookie(os.environ["mycookie"])
-
-
3
Print the value. For testing purposes, you print out the cookie and display it on the user's screen. This lets you verify that the cookie was properly read. Add the following code to display the cookie value:
print cookievalue["session"].value
-
1