【问题标题】:Sending a http request with METHOD=HEAD [duplicate]使用 METHOD=HEAD 发送 http 请求 [重复]
【发布时间】:2011-12-03 07:28:56
【问题描述】:

我想使用 python 2 发送一个带有 METHOD=HEAD 的 http 请求。在 mechanize 中有一个很好的函数,叫做 mechanize.Request。不幸的是,我只能将 METHOD 设置为 GET 或 POST ,但没有别的。你知道有没有办法做到这一点?

【问题讨论】:

    标签: python mechanize


    【解决方案1】:

    使用这个:

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 2010-09-11
      • 2013-09-29
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多