34
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))    
opener.open('http://abc.com')
opener.open('http://google.com')

As you can see, I use opener to visit different websites, using a cookie jar. Can I set a header so that each time a website is it, the header is applied?

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

63

You can add the headers directly to the OpenerDirector object returned by build_opener. From the last example in the urllib2 docs:

OpenerDirector automatically adds a User-Agent header to every Request. To change this:

import urllib2
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http://www.example.com/')

Also, remember that a few standard headers (Content-Length, Content-Type and Host) are added when the Request is passed to urlopen() (or OpenerDirector.open()).

senderle
  • 145,869
  • 36
  • 209
  • 233
  • 6
    @PiotrDobrogost, to be fair, this particular functionality is underdocumented. The [`OpenerDirector`](http://docs.python.org/release/2.6/library/urllib2.html#urllib2.OpenerDirector) class entry is bare; the above information is _very_ easy to miss. – senderle Sep 17 '12 at 12:07
18
headers = {'foo': 'bar',}
req = urllib2.Request(url, None, headers)
resp = urllib2.urlopen(req)

or

req = urllib2.Request(url)
req.add_header('foo', 'bar')
resp = urllib2.urlopen(req)
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143