[Tutor] Using debuglevel with urllib2?

Kent Johnson kent37 at tds.net
Wed Nov 9 04:32:41 CET 2005

Terry Carroll wrote:
> With urllib, you can set httplib.HTTPConnection.debuglevel to see the HTTP 
> conversation.  But it doesn't work with urllib2.  Can someone explain how 
> to use it in conjunction with urllib2?

You have to create your own HTTPHandler, set it to debug and install it in urllib2. Like this:

 >>> import urllib2
 >>> h=urllib2.HTTPHandler(debuglevel=1)
 >>> opener = urllib2.build_opener(h)
 >>> urllib2.install_opener(opener)
 >>> urllib2.urlopen('http://www.google.com').read()
connect: (www.google.com, 80)
send: 'GET / HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: www.google.com\r\nConnection: close\r\nUser-agent: Python-urllib/2.
reply: 'HTTP/1.1 200 OK\r\n'
header: Cache-Control: private

> Leaving aside the issue of whether this actually is a bug or an 
> imporvement, I don't follow the explanation.

It's pretty obscure. I had to poke around in the source for a while before I could understand it.



More information about the Tutor mailing list