How do you get default headers in a urllib2 Request?

后端 未结 8 1149
花落未央
花落未央 2020-12-13 07:22

I have a Python web client that uses urllib2. It is easy enough to add HTTP headers to my outgoing requests. I just create a dictionary of the headers I want to add, and pa

8条回答
  •  星月不相逢
    2020-12-13 08:08

    If you want to see the literal HTTP request that is sent out, and therefore see every last header exactly as it is represented on the wire, then you can tell urllib2 to use your own version of an HTTPHandler that prints out (or saves, or whatever) the outgoing HTTP request.

    import httplib, urllib2
    
    class 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/')
    

    The result of running this code is:

    GET / HTTP/1.1
    Accept-Encoding: identity
    Host: www.google.com
    Connection: close
    User-Agent: Python-urllib/2.6
    

提交回复
热议问题