[Tutor] Using debuglevel with urllib2?

Terry Carroll carroll at tjc.com
Wed Nov 9 01:49:55 CET 2005


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?


Longer story: debuuglevel is a nice debug tool that lets you see the 
client-server HTTP conversation:

>>> import httplib
>>> httplib.HTTPConnection.debuglevel = 1
>>> import urllib
>>> testurl = "http://www.yahoo.com"
>>> f = urllib.urlopen(testurl)
connect: (www.yahoo.com, 80)
send: 'GET / HTTP/1.0\r\nHost: www.yahoo.com\r\nUser-agent: Python-urllib/1.16\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Wed, 09 Nov 2005 00:37:50 GMT
header: P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
header: Cache-Control: private
header: Vary: User-Agent
header: Set-Cookie: FPB=pkvre11vo11n2h6u; expires=Thu, 01 Jun 2006 19:00:00 GMT; path=/; domain=www.yahoo.com
header: Connection: close
header: Content-Type: text/html
>>>


Alas, it doesn't work with urllib2:

>>> import urllib2
>>> f = urllib2.urlopen(testurl)
>>>

[silence]

I need to use urllib2, because I need to include some headers, and as far 
as I can tell, urllib doesn't let you do that.

The debuglevel issue was reported as a bug, 1152723,[1] but one of the
comments is that the h.set_debuglevel(self._debuglevel) can be used to
address this, on a per-connection basis.

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

Can someone explain to me how to set the debuglevel using urllib2?

Thanks!


[1] Link to bug report:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1152723&group_id=5470
or  http://makeashorterlink.com/?F23C2102C




More information about the Tutor mailing list