Python urllib2.Request.get_header documentation?
I'm working on a library that involves pre-processing of urllib2.Request instances (using the urllib2.BaseHandler.xxx_request callbacks). Some of the pre-processors need to examine the headers contained in the supplied urllib2.Request instance.
I noticed the official Python documentation only lists methods for adding a header and for checking if a header exists. There is an undocumented urllib2.Request.get_header method, though it seems to have some quirks. For example, it changes the case of multi-word headers:
from urllib2 import Request
req = Request('http://www.example.com')
req.add_header('Content-Type', 'application/x-www-form-urlencoded')
req.get_header('Content-Type') # Produces noth开发者_开发技巧ing
req.get_header('Content-type') # Produces 'application/x-www-form-urlencoded'
Is get_header officially supported and/or documented anywhere? If not, is there a best practice for reading header values from a urllib2.Request instance?
I'm sure get_header
is officially supported, but I can't find it mentioned in the documentation.
As to your "get_header returning None" problem, looking at the source to urllib2.py, the add_header
method is calling key.capitalize()
before saving the header key-value pair, which is why get_header('Content-type')
works and the other variants do not:
class Request:
def add_header(self, key, val):
self.headers[key.capitalize()] = val
>>> 'content-type'.capitalize()
'Content-type'
精彩评论