简体   繁体   English

如何捕获异常MaxRetryError?

[英]How to catch exception MaxRetryError?

I have a list of proxy addresses for which I want to connect the telegrams-bot. 我有一个要连接电报机器人的代理地址列表。 Proxies can be blocked over time or simply not work, this can be seen by the MaxRetryError error. 代理可能会随着时间的流逝而阻塞,或者根本无法正常工作,这可以通过MaxRetryError错误看到。 But I can't catch an error. 但是我找不到错误。 I have an error in logs. 我在日志中有一个错误。 I want to catch an error and switch to another proxy server. 我想捕获一个错误并切换到另一个代理服务器。

from telegram.ext import Updater

REQUEST_KWARGS = {
    'proxy_url': proxy_url,
    'urllib3_proxy_kwargs': {
        'retries': 0
    }
}

updater = Updater(token=TELEGRAM_TOKEN, request_kwargs=REQUEST_KWARGS)
queue = updater.start_polling(bootstrap_retries=0)

.....
raise MaxRetryError(_pool, url, error or ResponseError(cause))
telegram.vendor.ptb_urllib3.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot877422445:AAEGBD0D9stJKMF6PvCClChx8MNMGX-vLEY/deleteWebhook (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x11572df98>: Failed to establish a new connection: [Errno 61] Connection refused')))
......
telegram.error.NetworkError: urllib3 HTTPError HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot877422445:AAEGBD0D9stJKMF6PvCClChx8MNMGX-vLEY/deleteWebhook (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<telegram.vendor.ptb_urllib3.urllib3.connection.VerifiedHTTPSConnection object at 0x11572df98>: Failed to establish a new connection: [Errno 61] Connection refused')))

2019-07-26 16:07:38,553 - telegram.ext.dispatcher - CRITICAL - stopping due to exception in another thread

This way ? 这样

try:
  updater = Updater(token=TELEGRAM_TOKEN, request_kwargs=REQUEST_KWARGS)
  queue = updater.start_polling(bootstrap_retries=0)
except MaxRetryError:
  #doSomething

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM