python - 使用 METHOD=HEAD 发送 http 请求

标签 python mechanize

这个问题在这里已经有了答案:





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/

相关文章:

python - pandas.read_sql 处理速度

python - 谷歌驱动器用户界面和 API 之间的谷歌驱动器使用统计差异

python - Mechanize [Errno 10054] 现有连接被远程主机强行关闭

Python session 、cookie 和网络

ruby-on-rails - 如何将 Nokogiri 语句转换为 Mechanize 以进行屏幕抓取?

python - 使用 Tkinter 在闪烁的光标下方打开一个新窗口

python - 基本 Django 搜索实现

python - Google App Engine 中的工作日期时间对象

python - BeautifulSoup 未正确获取所有 HTML

python - <unknown GET form encoding type 'multipart/form-data' > 使用 Python 的 Mechanize