我知道如何获取标题RECEIVED
resp = urllib2.urlopen('http://www.google.com')
print resp.info()
但是如何将标题SENT访问为“http://www.google.com”?
我通常使用wireshark来分析和查看实际发送的内容,但我希望能够在我的脚本中访问这些信息。
答案 0 :(得分:2)
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/')
答案 1 :(得分:0)
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