【问题标题】:Problems with hooks using Requests Python package使用 Requests Python 包的钩子问题
【发布时间】:2013-03-13 16:58:04
【问题描述】:

我正在使用模块 requests,当我开始使用钩子时收到了这条消息。

File "/Library/Python/2.7/site-packages/requests-1.1.0-py2.7.egg/requests/sessions.py", line 321, in request
resp = self.send(prep, **send_kwargs)

File "/Library/Python/2.7/site-packages/requests-1.1.0-py2.7.egg/requests/sessions.py", line 426, in send
r = dispatch_hook('response', hooks, r, **kwargs)

File "/Library/Python/2.7/site-packages/requests-1.1.0-py2.7.egg/requests/hooks.py", line 41, in dispatch_hook
_hook_data = hook(hook_data, **kwargs)
TypeError: hook() got an unexpected keyword argument 'verify'

这是我的代码(简化版):

import requests
def hook(r):
     print r.json()

r = requests.get("http://search.twitter.com/search.json?q=blue%20angels&rpp=5", hooks=dict(response=hook))

【问题讨论】:

  • 我认为您需要检查服务器发送给您的内容。
  • 没有钩子的相同代码可以正常工作:import requestsr = requests.get("http://search.twitter.com/search.json?q=blue%20angels&rpp=5")print r.json()

标签: python python-2.7 python-3.x request typeerror


【解决方案1】:

根据the requests documentation,你的钩子函数不需要任何关键字参数,但根据to the source code on github,事件调度器可能会将kwargs传递给你的钩子函数。对我来说,这似乎是不完整的文档。将您的方法重新定义为:

def hook(r, **kwargs):
    # ...

【讨论】:

    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多