【问题标题】:How do I get HTTP VERB from Python Requests 'Response' objects?如何从 Python 请求“响应”对象中获取 HTTP VERB?
【发布时间】:2013-03-08 11:59:32
【问题描述】:

从查看他们的来源看来,method 成员属性是我想要的。

https://github.com/kennethreitz/requests/blob/master/requests/models.py

举例来说,这就是我想要的:

>>> r = requests.get("http://httpbin.org/get")
>>> print r.method
'GET'

但是我不知道是否有办法获得它(无需编写我自己的 hacky 包装器)...

【问题讨论】:

  • 不,原始请求动词不存储在响应中。为什么不存储当你提出请求时
  • 你的意思是打电话给my_fun(func=requests.get, verb='GET', url),然后在def my_fun(func, verb, url):里面有print verb; return func(url)
  • 或者func.__name__.upper() 可能(根本不使用verb),但是可以。
  • 啊,总是忘记 Python 中的内省(我是老派 C++ 爱好者);感谢您的提示:)
  • 但是,使用 request 属性(我很方便地忘记了),Jon 的回答对您来说更方便。就用那个吧。

标签: python wrapper python-requests urllib3


【解决方案1】:

它存储在响应的request 属性中:

>>> r = requests.head('http://www.example.com')
>>> r.request.method
'HEAD'

>>> r = requests.get('http://www.example.com')
>>> r.request.method
'GET'

【讨论】:

  • 完美,正是我想要的:D
猜你喜欢
  • 2016-08-25
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 2012-03-26
  • 2020-10-17
相关资源
最近更新 更多