完全禁用grequests日志记录

时间:2015-12-24 23:53:40

标签: python python-requests grequests

如何抑制我从Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run result = self._run(*self.args, **self.kwargs) File "/usr/local/lib/python2.7/dist-packages/grequests.py", line 71, in send self.url, **merged_kwargs) File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 456, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 559, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 384, in send raise Timeout(e, request=request) Timeout: HTTPSConnectionPool(host='itunes.apple.com', port=443): Read timed out. <Greenlet at 0x7ff0c8165910: <bound method AsyncRequest.send of <grequests.AsyncRequest object at 0x7ff0c880b8d0>>(stream=False)> failed with Timeout 获得的以下调试错误消息?

# disable requests/grequests logging
logging.getLogger("requests").setLevel(logging.CRITICAL)
logging.getLogger("grequests").setLevel(logging.CRITICAL)
logging.getLogger("urllib3").setLevel(logging.CRITICAL)
logging.getLogger("gevent").setLevel(logging.CRITICAL)

这是我到目前为止所尝试的内容:

rs = (grequests.get(url, headers=headers, cookies=cookies, proxies=proxies, timeout=timeout) for url in urls)
res_items = grequests.map(rs) # <-- this command produces all the errors

此处发生错误:

log.debug

我收到数以千计的这些消息,我不想看到。我也在很多其他地方使用grequests所以不想在程序范围内更改调试级别。如何仅抑制g调试消息?

1 个答案:

答案 0 :(得分:0)

为此你需要做三件事:

(1)确保您安装了最新版本的grequests

sudo pip install git+https://github.com/kennethreitz/grequests.git

(2)拥有grequests.map功能的处理程序:

def exception_handler(request, exception):
    pass # suppress errors on grequests.map

res_items = grequests.map(rs, exception_handler=exception_handler)

(3)取消urllib3警告:

import urllib3
urllib3.disable_warnings()
logging.captureWarnings(True)

现在,grequests不应再发出任何警告。