电报bot中的网址超出最大重试次数 [英] max retry exceeded with url in telegram bot

查看:303
本文介绍了电报bot中的网址超出最大重试次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用python编写电报bot.但是当我在一段时间后运行它时,它返回错误:

I started writing a telegram bot in python. but when I run it after a while it returns an error:

Exception in thread updater: Traceback (most recent call last):   File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 137, in
_new_conn
    (self.host, self.port), self.timeout, **extra_kw)   File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 91, in create_connection
    raise err   File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 81, in create_connection
    sock.connect(sa) OSError: [Errno 101] Network is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 559, in urlopen
    body=body, headers=headers)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 345, in _make_request
    self._validate_conn(conn)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 786, in _validate_conn
    conn.connect()   File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 217, in connect
    conn = self._new_conn()   File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 146, in
_new_conn
    self, "Failed to establish a new connection: %s" % e) urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7f2c694fa240>: Failed to establish a new connection: [Errno 101] Network is unreachable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()   File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)   File "/usr/local/lib/python3.5/dist-packages/python_telegram_bot-5.3.0-py3.5.egg/telegram/ext/updater.py", line 122, in _thread_wrapper
    target(*args, **kwargs)   File "/usr/local/lib/python3.5/dist-packages/python_telegram_bot-5.3.0-py3.5.egg/telegram/ext/updater.py", line 258, in _start_polling
    allowed_updates=allowed_updates)   File "/usr/local/lib/python3.5/dist-packages/python_telegram_bot-5.3.0-py3.5.egg/telegram/bot.py", line 125, in decorator
    result = func(self, *args, **kwargs)   File "/usr/local/lib/python3.5/dist-packages/python_telegram_bot-5.3.0-py3.5.egg/telegram/bot.py", line 1313, in getUpdates
    result = self._request.post(url, data, timeout=float(read_latency) + float(timeout))   File "/usr/local/lib/python3.5/dist-packages/python_telegram_bot-5.3.0-py3.5.egg/telegram/utils/request.py", line 243, in post
    **urlopen_kwargs)   File "/usr/local/lib/python3.5/dist-packages/python_telegram_bot-5.3.0-py3.5.egg/telegram/utils/request.py", line 165, in _request_wrapper
    resp = self._con_pool.request(*args, **kwargs)   File "/usr/lib/python3/dist-packages/urllib3/request.py", line 73, in request
    **urlopen_kw)   File "/usr/lib/python3/dist-packages/urllib3/request.py", line 151, in request_encode_body
    return self.urlopen(method, url, **extra_kw)   File "/usr/lib/python3/dist-packages/urllib3/poolmanager.py", line 162, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 629, in urlopen
    release_conn=release_conn, **response_kw)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 629, in urlopen
    release_conn=release_conn, **response_kw)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 629, in urlopen
    release_conn=release_conn, **response_kw)   File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 609, in urlopen
    _stacktrace=sys.exc_info()[2])   File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 273, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot***********************************/getUpdates (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f2c694fa240>: Failed to establish a new connection: [Errno 101] Network is unreachable',))

我在互联网上进行了搜索,但没有发现任何有用的信息.谁能帮助我处理或解决此错误?

I searched in the internet but I found nothing useful. can anyone help me to handle this error or solve it?

推荐答案

我遇到了同样的问题.实际上,这意味着Telegram由于请求过多而拒绝了您的连接. 在此处进行检查(相同的问题,但带有Itunes).

I was facing the same problem. What it actually means is that Telegram in this case is refusing your connection due to too many requests. Check it here (same question but with Itunes).

尝试使用try-except以避免此错误消息.

Try using a try-except to avoid this error message.

try:
    page1 = #whatever code
except requests.exceptions.ConnectionError:
    r.status_code = "Connection refused"

大多数情况可能是因为您让它运行但未在Bot中编写任何内容,所以它变得无声".如果我们休眠"程序,则不会发生,因为不会发送空请求.在python中使用sleep(timeinsec)函数(不要忘记导入sleep).

And most of it might be because you let it run but not write anything in the bot, so it goes "silent". If we "sleep" the program, that won't happen as empty requests won't be sent. Use sleep(timeinsec) function in python (don't forget to import sleep).

from time import sleep

这篇关于电报bot中的网址超出最大重试次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆