如果您想查看发送的文字HTTP请求,并因此而看到每个最后一个标头,都与线路上的表示完全相同,那么您可以告诉
urllib2您使用自己的版本(
HTTPHandler可以打印(或保存)或其他任何形式)传出的HTTP请求。
import httplib, urllib2class MyHTTPConnection(httplib.HTTPConnection): def send(self, s): print s # or save them, or whatever! httplib.HTTPConnection.send(self, s)class MyHTTPHandler(urllib2.HTTPHandler): def http_open(self, req): return self.do_open(MyHTTPConnection, req)opener = urllib2.build_opener(MyHTTPHandler)response = opener.open('http://www.google.com/')运行此代码的结果是:
GET / HTTP/1.1Accept-Encoding: identityHost: www.google.comConnection: closeUser-Agent: Python-urllib/2.6



