这个问题在这里已经有了答案:
How do you send a HEAD HTTP request in Python 2?
(11 个回答)
去年关闭。
我想使用 python 2 发送带有 METHOD=HEAD 的 http 请求。在 mechanize 中有一个很好的函数,称为 mechanize.Request。不幸的是,我只能将 METHOD 设置为 GET 或 POST,但没有其他设置。你知道是否有办法做到这一点?
最佳答案
用这个:
import urllib2
class RequestWithMethod(urllib2.Request):
def __init__(self, method, *args, **kwargs):
self._method = method
urllib2.Request.__init__(*args, **kwargs)
def get_method(self):
return self._method
然后做这样的事情:
request = RequestWithMethod("HEAD", "%s" % url)
关于python - 使用 METHOD=HEAD 发送 http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7692731/