Elias Zamaria
Elias Zamaria

Reputation: 101053

Any way to set request headers when doing a request using urllib in Python 2.x?

I am trying to make an HTTP request in Python 2.6.4, using the urllib module. Is there any way to set the request headers?

I am sure that this is possible using urllib2, but I would prefer to use urllib since it seems simpler.

Upvotes: 0

Views: 890

Answers (3)

Michael
Michael

Reputation: 9058

You could actually overwrite methods in FancyURLopener, but that does not seem right (see http://docs.python.org/library/urllib.html#urllib._urlopener). You could also use httplib which basically is the backend for urllib.

But anyhow, using urllib2 might be the best idea. It is not difficult to use. (I use it all the time.)

Upvotes: 1

Andrew McGregor
Andrew McGregor

Reputation: 34592

There isn't any way to do that, which is precisely the reason urllib is deprecated in favour of urllib2. So just use urllib2 rather than writing new code to a deprecated interface.

Upvotes: 2

AndiDog
AndiDog

Reputation: 70108

I don't think so, but urllib2 can. Check out the documentation of urllib2.Request.

Upvotes: 1

Related Questions