[英]Any way to set request headers when doing a request using urllib in Python 2.x?
I am trying to make an HTTP request in Python 2.6.4, using the urllib module. 我正在尝试使用urllib模块在Python 2.6.4中发出HTTP请求。 Is there any way to set the request headers?
有什么方法可以设置请求标头?
I am sure that this is possible using urllib2, but I would prefer to use urllib since it seems simpler. 我确信使用urllib2可以做到这一点,但是我更喜欢使用urllib,因为它看起来更简单。
There isn't any way to do that, which is precisely the reason urllib
is deprecated in favour of urllib2
. 没有任何方法可以做到这一点,这正是
urllib
被赞成使用urllib2
的原因。 So just use urllib2
rather than writing new code to a deprecated interface. 因此,仅使用
urllib2
而不是将新代码编写到不推荐使用的接口中。
I don't think so, but urllib2 can. 我不这么认为,但是urllib2可以。 Check out the documentation of urllib2.Request .
查看urllib2.Request的文档 。
You could actually overwrite methods in FancyURLopener
, but that does not seem right (see http://docs.python.org/library/urllib.html#urllib._urlopener ). 您实际上可以覆盖
FancyURLopener
方法,但这似乎不正确(请参见http://docs.python.org/library/urllib.html#urllib._urlopener )。 You could also use httplib
which basically is the backend for urllib
. 您还可以使用
httplib
,它基本上是urllib
的后端。
But anyhow, using urllib2
might be the best idea. 但是无论如何,使用
urllib2
可能是最好的主意。 It is not difficult to use. 使用起来并不难。 (I use it all the time.)
(我用它所有的时间。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.