我开始在python中编写电报机器人。但是当我在一段时间后运行它会返回错误:
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',))
我在互联网上搜索但我发现没什么用处。任何人都可以帮我处理这个错误或解决它吗?
答案 0 :(得分:1)
我面临着同样的问题。实际上,这意味着Telegram由于请求过多而拒绝了您的连接。 Check it here(相同的问题,但有Itunes)。
尝试使用try-except以避免此错误消息。
try:
page1 = #whatever code
except requests.exceptions.ConnectionError:
r.status_code = "Connection refused"
大多数情况可能是因为您让它运行但不向bot写任何东西,所以它变得“无声”。如果我们“睡眠”程序,则不会发生,因为不会发送空请求。在python中使用sleep(timeinsec)函数(不要忘记导入sleep)。
from time import sleep