Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

urllib2 with cookies

I am trying to make a request to an RSS feed that requires a cookie, using python. I thought using urllib2 and adding the appropriate heading would be sufficient, but the request keeps saying unautherized.

Im guessing it could be a problem on the remote sites' side, but wasnt sure. How do I use urllib2 along with cookies? is there a better package for this (like httplib, mechanize, curl)

like image 996
neolaser Avatar asked Jan 04 '12 22:01

neolaser


1 Answers

I would use requests package, docs, it's a lot easier to use than urlib2 (sane API).

If a response contains some Cookies, you can get quick access to them:

url = 'http://httpbin.org/cookies/set/requests-is/awesome'
r = requests.get(url)
print r.cookies #{'requests-is': 'awesome'}

To send your own cookies to the server, you can use the cookies parameter:

url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
r.content # '{"cookies": {"cookies_are": "working"}}'

http://docs.python-requests.org/en/latest/user/quickstart/#cookies

like image 90
clyfe Avatar answered Oct 11 '22 13:10

clyfe