發送了頭,我知道如何讓頭收到獲取的urllib2 http請求
resp = urllib2.urlopen('http://www.google.com')
print resp.info()
但你如何獲得發送到「http://www.google.com」的標題?
我通常使用wireshark來分析並查看實際發送的內容,但我想在腳本中訪問這些信息。
發送了頭,我知道如何讓頭收到獲取的urllib2 http請求
resp = urllib2.urlopen('http://www.google.com')
print resp.info()
但你如何獲得發送到「http://www.google.com」的標題?
我通常使用wireshark來分析並查看實際發送的內容,但我想在腳本中訪問這些信息。
import httplib
import urllib2
class CustomHTTPConnection(httplib.HTTPConnection):
def request(self, method, url, body=None, headers={}):
print headers
self._send_request(method, url, body, headers)
class CustomHTTPHandler(urllib2.AbstractHTTPHandler):
def http_open(self, req):
return self.do_open(CustomHTTPConnection, req)
http_request = urllib2.AbstractHTTPHandler.do_request_
if __name__ == '__main__':
opener = urllib2.OpenerDirector()
opener.add_handler(CustomHTTPHandler())
res = opener.open('http://www.google.it/')
import httplib
import urllib2
class CustomHTTPConnection(httplib.HTTPConnection):
def request(self, method, url, body=None, headers={}):
self.req_headers = headers
self._send_request(method, url, body, headers)
def getresponse(self, buffering=False):
resp = httplib.HTTPConnection.getresponse(self, buffering)
for key, value in self.req_headers.items():
resp.msg.headers.append('req_%s: %s\r\n' % (key, value))
return resp
class CustomHTTPHandler(urllib2.AbstractHTTPHandler):
def http_open(self, req):
resp = self.do_open(CustomHTTPConnection, req)
return resp
http_request = urllib2.AbstractHTTPHandler.do_request_
if __name__ == '__main__':
opener = urllib2.OpenerDirector()
opener.add_handler(CustomHTTPHandler())
res = opener.open('http://www.google.it/')
info = res.info()
print info
看到這裏如何檢查實際的請求:http://stackoverflow.com/questions/603856/how-do-you-get-default-headers-in-a-urllib2-request – 2011-05-22 02:55:08