Reading Cookie from Python

1,947 views
Skip to first unread message

Reetesh Nigam

unread,
Nov 5, 2013, 3:40:22 AM11/5/13
to python_in...@googlegroups.com
Hi All,

I need to read cookie using Python api. I have tried below library:
1. webdriver of selenium
2. urllib2
3. cookie jar.

for all the above library I am getting different cookie id if I fire same URL using above code.

Note : If I use browser and fire same command I get same cookie id. below are the code.. 

from selenium import webdriver
driver = webdriver.Firefox()

driver.get("abc.com")

cookies = driver.get_cookies()
for cookie in cookies:
    print(cookie['name'] + ' --> ' + cookie['value'])
    print cookie['domain']
    print cookie['expiry']
    print cookie['path']
print "*=*"*10  

============================

from urllib2 import Request, build_opener, HTTPCookieProcessor, HTTPHandler
import cookielib
cj = cookielib.CookieJar()
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())
req = Request("abc.com")
f = opener.open(req)
print "the cookies are: "
for cookie in cj:
    print cookie

Justin Israel

unread,
Nov 5, 2013, 5:03:22 AM11/5/13
to python_in...@googlegroups.com
This is an absolute shot in the dark... but, does it have anything to do with possibly lacking a session in your http requests? 
The python "requests" module supports sessions:

Is that what you are looking for? Is the problem that you are not seeing the cookies persist?


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2ddd33ff-eb91-49c4-bd0b-4e463049f6b3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reetesh Nigam

unread,
Nov 6, 2013, 2:14:24 AM11/6/13
to python_in...@googlegroups.com
Hi Justin,

The cookie which I am getting in the code is not reflecting in server side : i.e if it generate cookie : x then its not visible in server where I am sending my request. 
Does python request behave like curl command?

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Nov 6, 2013, 3:41:52 AM11/6/13
to python_in...@googlegroups.com
All I know is that per the examples showed with the requests module, the cookies created on the client side are visible on the server and returned to the client.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3498360a-97c8-4356-a2f1-1b715f7c10d8%40googlegroups.com.

Jesse Kretschmer

unread,
Nov 6, 2013, 1:18:17 PM11/6/13
to python_in...@googlegroups.com
Reetesh,
I also looked over your urllib2 example. It seems like it should work. 

When I have to deal with a login which handles the session through a cookie, I will just install the opener globally to make it easier to deal with. After I run urllib2.install_opener(opener), I can simply call urllib2.urlopen() without worrying about setting up the cookies or interfacing with the opener that was built.

For us, it would be helpful to know why you are inspecting cookies? What's the end goal? There may well be a different solution than dealing with the specific cookie problem.

For you, it might be helpful to understand exactly what cookies are (http://en.wikipedia.org/wiki/HTTP_cookie). To paraphrase, the server will send a cookie key/value pair back to the client. From then on the client is expected to send that cookie back to the server as part of each request. This is exactly why the opener is necessary. It will handle the persistent client information.

I just dug up a recent script that I am using urllib2 with. It's a one-off script that I hashed out without much care, but I think it's legible. 

For those that don't want to read the source, it's a downloader for VRay nightlies. My studio has been doing some serious testing of the GPU rendering, and we always want the latest build available.

Cheers,
jesse

Jesse Kretschmer

unread,
Nov 6, 2013, 1:36:21 PM11/6/13
to python_in...@googlegroups.com
Also, if you don't mind getting nitty-gritty, you can run wireshark or some other sniffer to inspect the communications from the client to the server. You should be able to trap the http requests. Since the cookies are part of the request, you can read them in plain text.

This post (http://stackoverflow.com/questions/1170744) also suggests that you can do the inspection with urllib2 directly.
Reply all
Reply to author
Forward
0 new messages