Python 发送方法为HEAD的http请求

Python 发送方法为HEAD的http请求,python,mechanize,Python,Mechanize,我想使用Python2发送一个METHOD=HEAD的http请求。在mechanize中有一个很好的函数叫做mechanize.Request。不幸的是,我只能将方法设置为GET或POST,而不能设置其他内容。你知道是否有办法做到这一点吗?使用以下方法: import urllib2 class RequestWithMethod(urllib2.Request): def __init__(self, method, *args, **kwargs): self._method

我想使用Python2发送一个METHOD=HEAD的http请求。在mechanize中有一个很好的函数叫做mechanize.Request。不幸的是,我只能将方法设置为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)