Python 电报bot中url超过最大重试次数
我开始用python编写一个电报机器人。但当我在一段时间后运行它时,它会返回一个错误:Python 电报bot中url超过最大重试次数,python,python-requests,bots,telegram,Python,Python Requests,Bots,Telegram,我开始用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)
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',))
线程更新程序中的异常:回溯(最后一次调用):文件“/usr/lib/python3/dist-packages/urllib3/connection.py”,第137行,在
_新康涅狄格州
(self.host,self.port),self.timeout,**extra_-kw)文件“/usr/lib/python3/dist-packages/urllib3/util/connection.py”,第91行,在create_-connection中
在create_connection中的第81行,提出错误文件“/usr/lib/python3/dist packages/urllib3/util/connection.py”
sock.connect(sa)操作错误:[Errno 101]无法访问网络
在处理上述异常期间,发生了另一个异常:
回溯(最近一次调用):urlopen中的文件“/usr/lib/python3/dist packages/urlib3/connectionpool.py”,第559行
body=body,headers=headers)文件“/usr/lib/python3/dist-packages/urlib3/connectionpool.py”,第345行,在请求中
self.\u validate\u conn(conn)文件“/usr/lib/python3/dist packages/urllib3/connectionpool.py”,第786行,在\u validate\u conn中
connect()文件“/usr/lib/python3/dist packages/urllib3/connection.py”,第217行,在connect中
conn=self._new_conn()文件“/usr/lib/python3/dist packages/urllib3/connection.py”,第146行,在
_新康涅狄格州
self,“未能建立新连接:%s”%e)urllib3.exceptions.NewConnectionError::未能建立新连接:[Errno 101]无法访问网络
在处理上述异常期间,发生了另一个异常:
回溯(最近一次调用):文件“/usr/lib/python3.5/threading.py”,第914行,在bootstrap\u内部
self.run()文件“/usr/lib/python3.5/threading.py”,第862行,在run中
self.\u target(*self.\u args,**self.\u kwargs)文件“/usr/local/lib/python3.5/dist packages/python\u telegram\u bot-5.3.0-py3.5.egg/telegram/ext/updater.py”,第122行,在线程包装器中
target(*args,**kwargs)文件“/usr/local/lib/python3.5/dist-packages/python\u-telegram\u bot-5.3.0-py3.5.egg/telegram/ext/updater.py”,第258行,在开始轮询中
允许的\u更新=允许的\u更新)文件“/usr/local/lib/python3.5/dist packages/python\u电报\u bot-5.3.0-py3.5.egg/telegate/bot.py”,第125行,在decorator中
结果=func(self,*args,**kwargs)文件“/usr/local/lib/python3.5/dist-packages/python\u-telegram\u-bot-5.3.0-py3.5.egg/telegram/bot.py”,第1313行,在GetUpdate中
结果=self.\u request.post(url,数据,超时=浮点(读取延迟)+浮点(超时))文件“/usr/local/lib/python3.5/dist packages/python\u telegram\u bot-5.3.0-py3.5.egg/telegram/utils/request.py”,post第243行
**urlopen_kwargs)文件“/usr/local/lib/python3.5/dist-packages/python_-telegate_-bot-5.3.0-py3.5.egg/telegate/utils/request.py”,第165行,在请求包装中
resp=self.\u con_pool.request(*args,**kwargs)文件“/usr/lib/python3/dist packages/urllib3/request.py”,请求中的第73行
**urlopen_kw)文件“/usr/lib/python3/dist packages/urllib3/request.py”,第151行,在请求编码_正文中
在urlopen的第162行返回self.urlopen(方法,url,**extra_kw)文件“/usr/lib/python3/dist packages/urllib3/poolmanager.py”
response=conn.urlopen(方法,u.request_uri,**kw)文件“/usr/lib/python3/dist packages/urllib3/connectionpool.py”,第629行,在urlopen中
release\u conn=release\u conn,**响应\u kw)文件“/usr/lib/python3/dist packages/urlib3/connectionpool.py”,第629行,在urlopen中
release\u conn=release\u conn,**响应\u kw)文件“/usr/lib/python3/dist packages/urlib3/connectionpool.py”,第629行,在urlopen中
release\u conn=release\u conn,**响应\u kw)文件“/usr/lib/python3/dist packages/urlib3/connectionpool.py”,第609行,在urlopen中
_stacktrace=sys.exc_info()
raise MaxRetryError(_pool,url,error or ResponseError(cause))urlib3.exceptions.MaxRetryError:HTTPSConnectionPool(host='api.telegrame.org',port=443):url超过了最大重试次数:/bot*********************************************/getUpdates(由NewConnectionError引起(':无法建立新连接:[Errno 101]网络无法访问',))
我在网上搜索了一下,但没有发现任何有用的东西。有人能帮我处理或解决这个错误吗?我也面临同样的问题。实际上它的意思是,在这种情况下,电报由于请求太多而拒绝您的连接。(与Itunes相同的问题) 尝试使用Try-except以避免此错误消息
try:
page1 = #whatever code
except requests.exceptions.ConnectionError:
r.status_code = "Connection refused"
大部分可能是因为您让它运行,但没有在bot中写入任何内容,所以它会“沉默”。如果我们“休眠”程序,那就不会发生,因为不会发送空请求。在python中使用sleep(timeinsec)函数(不要忘记导入睡眠)
始终从上到下读取错误代码。您的错误并不是因为超过了最大重试次数(正如您在问题标题中提到的),而是
OSError:[Errno 101]无法访问网络。其他错误都是由这个错误引起的。因此,您显然面临着一个网络(设置)问题。Ps:try和google forpython-urllib-OSError:[Errno 101]无法访问网络
from time import sleep