如何在请求期间禁止http.client异常日志记录。使用Python 3获取()请求

如何在请求期间禁止http.client异常日志记录。使用Python 3获取()请求,python,python-3.x,logging,python-requests,Python,Python 3.x,Logging,Python Requests,我正在使用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:

我正在使用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:ConnectionError!
回溯(最近一次呼叫最后一次):
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/urllib3/connectionpool.py”,urlopen中第601行
分块=分块)
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/urllib3/connectionpool.py”,第387行,在请求中
六、从(e,无)中提高
文件“”,第2行,从
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/urllib3/connectionpool.py”,第383行,在请求中
httplib_response=conn.getresponse()
文件“/usr/lib/python3.6/http/client.py”,第1331行,在getresponse中
response.begin()
文件“/usr/lib/python3.6/http/client.py”,第297行,在begin中
版本、状态、原因=self.\u读取\u状态()
文件“/usr/lib/python3.6/http/client.py”,第266行,处于读取状态
升起RemoteDisconnected(“远程端关闭连接,不带”
http.client.RemoteDisconnected:远程端关闭连接,无响应
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/adapters.py”,第440行,在send中
超时=超时
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/urllib3/connectionpool.py”,第639行,在urlopen中
_stacktrace=sys.exc_info()[2])
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/urllib3/util/retry.py”,第388行,增量
引发MaxRetryError(_池、url、错误或响应错误(原因))
urllib3.exceptions.MaxRetryError:HTTPSConnectionPool(host='www.thesite.com',port=443):url:/asdzxcqwe/2058706超过了最大重试次数(由ProxyError('无法连接到代理'),RemoteDisconnected('远程端关闭连接,无响应',)引起)
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/opt/scripts/this is my app/fetcher.py”,第417行,在make_请求中
resp=self.s.get(url,proxies=self.proxies)
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/sessions.py”,get中第521行
返回self.request('GET',url,**kwargs)
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/sessions.py”,请求中的第508行
resp=自我发送(准备,**发送)
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/sessions.py”,第640行,在send中
历史记录=[resp for resp in gen]如果允许,则重定向else[]
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/sessions.py”,第640行,在
历史记录=[resp for resp in gen]如果允许,则重定向else[]
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/sessions.py”,第218行,在resolve_重定向中
**转接器
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/sessions.py”,第618行,在send中
r=适配器.send(请求,**kwargs)
文件“/opt/scripts/this is my app/.venv/lib/python3.6/site packages/requests/adapters.py”,第502行,在send中
raise ProxyError(e,请求=请求)
requests.exceptions.ProxyError:HTTPSConnectionPool(host='www.thesite.com',port=443):url:/asdzxcqwe/2058706超过最大重试次数(由ProxyError('无法连接到代理')、RemoteDisconnected('远程端关闭连接而无响应',)引起)
但我想得到的只是记录的字符串:

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

你能告诉我我的错误在哪里吗?我怎样才能抑制那些异常文本并只获取我想要记录的文本


非常感谢

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

异常
记录消息和跟踪。所有其他日志方法仅以特定于该方法的优先级记录消息