How do you get default headers in a urllib2 Request?

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

提交回复
热议问题