gpt4 book ai didi

python - 如何在使用 Python 3 的 requests.get() 请求期间抑制 http.client 异常记录

转载 作者:太空宇宙 更新时间:2023-11-04 02:27:47 25 4
gpt4 key购买 nike

我正在使用 Python3 并使用以下代码发出请求:

      try:
resp = self.s.get(url, proxies=self.proxies)
return resp.text
except ConnectTimeout:
self.logger.exception('{}: connection timeout!'.format(self.name))
except ConnectionError:
self.logger.exception('{}: ConnectionError!'.format(self.name))
except RemoteDisconnected:
self.logger.exception('{}: RemoteDisconnected'.format(self.name))
except MaxRetryError:
self.logger.exception('{}: MaxRetryError'.format(self.name))
except Exception as e:
self.logger.exception(e)

如果我在 session().get(url, proxies=proxies) 期间遇到异常,我会在日志文件中得到以下信息:

2018-04-20 17:14:27 | RF5MR: ConnectionError!

Traceback (most recent call last):
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 601, in urlopen
chunked=chunked)
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 387, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
response.begin()
File "/usr/lib/python3.6/http/client.py", line 297, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.6/http/client.py", line 266, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/adapters.py", line 440, in send
timeout=timeout
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/urllib3/connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/urllib3/util/retry.py", line 388, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.thesite.com', port=443): Max retries exceeded with url: /asdzxcqwe/2058706 (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response',)))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/scripts/this-is-my-app/fetcher.py", line 417, in make_request
resp = self.s.get(url, proxies=self.proxies)
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/sessions.py", line 521, in get
return self.request('GET', url, **kwargs)
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/sessions.py", line 640, in send
history = [resp for resp in gen] if allow_redirects else []
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/sessions.py", line 640, in <listcomp>
history = [resp for resp in gen] if allow_redirects else []
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/sessions.py", line 218, in resolve_redirects
**adapter_kwargs
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/opt/scripts/this-is-my-app/.venv/lib/python3.6/site-packages/requests/adapters.py", line 502, in send
raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.thesite.com', port=443): Max retries exceeded with url: /asdzxcqwe/2058706 (Caused by ProxyError('Cannot connect to proxy.', RemoteDisconnected('Remote end closed connection without response',)))

但我想要得到的只是记录的那个字符串:

2018-04-20 17:14:27 | RF5MR:连接错误!

你能指出我的错误在哪里吗?我怎样才能抑制这些异常文本并只得到我想要记录的文本?

非常感谢!

最佳答案

不是调用 self.logger.exception,而是调用 self.logger.error

exception 记录消息和回溯。所有其他日志方法仅以特定于该方法的优先级记录消息。

参见 the documentation on the logging module .

关于python - 如何在使用 Python 3 的 requests.get() 请求期间抑制 http.client 异常记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49947016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com