2011-05-22 152 views
2

发送了头,我知道如何让头收到获取的urllib2 http请求

resp = urllib2.urlopen('http://www.google.com') 
print resp.info() 

但你如何获得发送到“http://www.google.com”的标题?

我通常使用wireshark来分析并查看实际发送的内容,但我想在脚本中访问这些信息。

+2

看到这里如何检查实际的请求:http://stackoverflow.com/questions/603856/how-do-you-get-default-headers-in-a-urllib2-request – 2011-05-22 02:55:08

回答

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/') 
+0

如何访问类之外的相同标题?除了打印出来之外,我还需要对头文件做些什么。 – Raj 2011-10-05 18:25:46

+0

不幸的是,没有使用全局变量不是一个简单的方法 ,因为opener.open返回HTTPResponse的HTTPMessage的addinfourl(这可能对你说什么)无论如何,你可以将头添加到响应,也许前缀与req_我添加代码作为另一个答案,或者有一种方法来格式化评论? – sherpya 2011-11-03 00:24:51

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