【问题标题】:How to catch exception MaxRetryError?如何捕获异常 MaxRetryError?
【发布时间】:2019-07-26 21:22:09
【问题描述】:

我有一个要连接电报机器人的代理地址列表。 代理可能会随着时间的推移而被阻止或根本无法工作,这可以通过 MaxRetryError 错误看出。 但我无法捕捉到错误。我在日志中有错误。 我想捕获一个错误并切换到另一个代理服务器。

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

【问题讨论】:

标签: python python-telegram-bot


【解决方案1】:

这个way ?

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

【讨论】:

  • 线程不会“死”,它在日志中被冻结了“telegram.ext.dispatcher - CRITICAL - 由于另一个线程中的异常而停止”
  • 你必须捕获线程异常telegram.error.NetworkError。这也是这个post的重复
  • thx,但它不起作用,尝试 - 除了任何“错误”都不起作用。也许有一些队列内部有错误并且没有放弃它“up”
猜你喜欢
  • 2017-03-14
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多