max retry exceeded with url in telegram bot

2019-09-14 16:22发布

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?

1条回答
戒情不戒烟
2楼-- · 2019-09-14 16:52

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 using a try-except to avoid this error message.

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

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
查看更多
登录 后发表回答