[转]解决在urllib2中无法显示调试信息问题

在urllib中,可以通过语句:httplib.HTTPConnection.debuglevel = 1
在控制台中输出调试信息
但是这个方法在urllib2中并不能奏效
解决方法是创建一个HTTPHandler,将它设置为debug模式并传递给urllib2,代码如下:
import urllib2
h = urllib2.HTTPHandler(debuglevel = 1)
request = urllib2.Request('http://www.baidu.com')
opener = urllib2.build_opener(h)
feeddata = opener.open(request).read()
输出信息:
connect: (www.baidu.com, 80)
send: 'GET / HTTP/1.1\r\n
Accept-Encoding: identity\r\n
Host: www.baidu.com\r\nConnection: close\r\n
User-Agent: Python-urllib/2.5\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Sun, 26 Dec 2010 11:41:36 GMT
header: Server: BWS/1.0
header: Content-Length: 6222
header: Content-Type: text/html;charset=gb2312
header: Cache-Control: private
header: Expires: Sun, 26 Dec 2010 11:41:36 GMT
header: Set-Cookie: BAIDUID=64F18F30196EC8FB1EA5F93447FEB0F2:FG=1; expires=Sun, 26-Dec-40 11:41:36 GMT; path=/; domain=.baidu.com
header: P3P: CP=" OTI DSP COR IVA OUR IND COM "
header: Connection: Close

你可能感兴趣的:(html,python,cache,sun)