How do you get default headers in a urllib2 Request?

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

    It sounds to me like you're looking for the headers of the response object, which include Connection: close, etc. These headers live in the object returned by urlopen. Getting at them is easy enough:

    from urllib2 import urlopen
    req = urlopen("http://www.google.com")
    print req.headers.headers
    

    req.headers is a instance of httplib.HTTPMessage

    0 讨论(0)
  • 2020-12-13 08:19

    see urllib2.py:do_request (line 1044 (1067)) and urllib2.py:do_open (line 1073) (line 293) self.addheaders = [('User-agent', client_version)] (only 'User-agent' added)

    0 讨论(0)
提交回复
热议问题