Python urllib2.Request.get_header 文档?
我正在开发一个涉及 urllib2.Request 实例预处理的库(使用 urllib2.BaseHandler.xxx_request 回调)。一些预处理器需要检查提供的 urllib2.Request 实例中包含的标头。
我注意到Python官方文档只列出了添加标头和检查标头是否存在的方法。有一个未记录的 urllib2.Request.get_header 方法,尽管它似乎有一些怪癖。例如,它更改了多字标头的大小写:
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 nothing
req.get_header('Content-type') # Produces 'application/x-www-form-urlencoded'
get_header 是否在任何地方得到正式支持和/或记录?如果没有,是否有从 urllib2.Request 实例读取标头值的最佳实践?
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 nothing
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确定
get_header
受到官方支持,但我找不到文档中提到的。至于“get_header returns None”问题,查看 urllib2.py 的源代码,
add_header
方法在保存标头键值之前调用key.capitalize()
这就是为什么get_header('Content-type')
有效而其他变体无效的原因: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 callingkey.capitalize()
before saving the header key-value pair, which is whyget_header('Content-type')
works and the other variants do not: