How do you get default headers in a urllib2 Request?

后端 未结 8 1150
花落未央
花落未央 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:10

    A other solution, witch used the idea from How do you get default headers in a urllib2 Request? But doesn't copy code from std-lib:

    class HTTPConnection2(httplib.HTTPConnection):
        """
        Like httplib.HTTPConnection but stores the request headers.
        Used in HTTPConnection3(), see below.
        """
        def __init__(self, *args, **kwargs):
            httplib.HTTPConnection.__init__(self, *args, **kwargs)
            self.request_headers = []
            self.request_header = ""
    
        def putheader(self, header, value):
            self.request_headers.append((header, value))
            httplib.HTTPConnection.putheader(self, header, value)
    
        def send(self, s):
            self.request_header = s
            httplib.HTTPConnection.send(self, s)
    
    
    class HTTPConnection3(object):
        """
        Wrapper around HTTPConnection2
        Used in HTTPHandler2(), see below.
        """
        def __call__(self, *args, **kwargs):
            """
            instance made in urllib2.HTTPHandler.do_open()
            """
            self._conn = HTTPConnection2(*args, **kwargs)
            self.request_headers = self._conn.request_headers
            self.request_header = self._conn.request_header
            return self
    
        def __getattribute__(self, name):
            """
            Redirect attribute access to the local HTTPConnection() instance.
            """
            if name == "_conn":
                return object.__getattribute__(self, name)
            else:
                return getattr(self._conn, name)
    
    
    class HTTPHandler2(urllib2.HTTPHandler):
        """
        A HTTPHandler which stores the request headers.
        Used HTTPConnection3, see above.
    
        >>> opener = urllib2.build_opener(HTTPHandler2)
        >>> opener.addheaders = [("User-agent", "Python test")]
        >>> response = opener.open('http://www.python.org/')
    
        Get the request headers as a list build with HTTPConnection.putheader():
        >>> response.request_headers
        [('Accept-Encoding', 'identity'), ('Host', 'www.python.org'), ('Connection', 'close'), ('User-Agent', 'Python test')]
    
        >>> response.request_header
        'GET / HTTP/1.1\\r\\nAccept-Encoding: identity\\r\\nHost: www.python.org\\r\\nConnection: close\\r\\nUser-Agent: Python test\\r\\n\\r\\n'
        """
        def http_open(self, req):
            conn_instance = HTTPConnection3()
            response = self.do_open(conn_instance, req)
            response.request_headers = conn_instance.request_headers
            response.request_header = conn_instance.request_header
            return response
    

    EDIT: Update the source

提交回复
热议问题