6

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)

neolaser
  • 6,722
  • 18
  • 57
  • 90
  • If all you want to know is how to set a cookie, [this question](http://stackoverflow.com/questions/3334809/python-urllib2-how-to-send-cookie-with-urlopen-request) provides the answer. – Emil Styrke Jan 04 '12 at 22:32
  • Thanks for the link. I checked it out and I am setting cookies in the same way (slightly different for each lib used) but still no luck – neolaser Jan 04 '12 at 22:58

2 Answers2

13

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

clyfe
  • 23,695
  • 8
  • 85
  • 109
  • 6
    requests is definitely much nicer, but you can do [something similar to your first example with the builtin urllib2 and cookielib](http://docs.python.org/library/cookielib.html#examples), if you have to – dbr Jan 04 '12 at 22:38
5
import urllib2
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', 'cookiename=cookievalue'))
f = opener.open("http://example.com/")
RanRag
  • 48,359
  • 38
  • 114
  • 167