python - 如何修复电报机器人中的连接错误?
问题描述
我开始编写一个电报机器人,并为此使用了 freeCodeCamp 示例。
from telegram.ext import Updater, CommandHandler
import requests
import re
contents = requests.get('https://random.dog/woof.json').json()
image_url = contents['url']
def get_url():
contents = requests.get('https://random.dog/woof.json').json()
url = contents['url']
return url
def get_image_url():
allowed_extension = ['jpg','jpeg','png']
file_extension = ''
while file_extension not in allowed_extension:
url = get_url()
file_extension = re.search("([^.]*)$",url).group(1).lower()
return url
def bop(bot, update):
url = get_url()
chat_id = update.message.chat_id
bot.send_photo(chat_id=chat_id, photo=url)
def main():
updater = Updater('<MY-PRIVATE-BOT-TOKEN>')
dp = updater.dispatcher
dp.add_handler(CommandHandler('bop', bop))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
运行此代码后,我收到此错误。此外,在运行我编写的任何电报机器人后,我都会收到此错误。
Traceback (most recent call last):
File "D:\workbot\venv\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "D:\workbot\venv\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\a\AppData\Local\Programs\Python\Python38\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11002] getaddrinfo failed
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='random.dog', port=443): Max retries exceeded with url: /woof.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026811517F10>: Failed to establish a new connection: [Errno 11002] getaddrinfo failed'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/workbot/main.py", line 5, in <module>
contents = requests.get('https://random.dog/woof.json').json()
File "D:\workbot\venv\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "D:\workbot\venv\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "D:\workbot\venv\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "D:\workbot\venv\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "D:\workbot\venv\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='random.dog', port=443): Max retries exceeded with url: /woof.json (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000026811517F10>: Failed to establish a new connection: [Errno 11002] getaddrinfo failed'))
由于过滤,我还必须使用代理或 VPN 连接到电报。
我在回声机器人中遇到了另一个连接错误。我使用了电报机器人文档代码。我收到了这个错误:
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000199A2C37DF0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/workbot/main.py", line 5, in <module>
contents = requests.get('https://google').json()
File "D:\workbot\venv\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "D:\workbot\venv\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "D:\workbot\venv\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "D:\workbot\venv\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "D:\workbot\venv\lib\site-packages\requests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='google', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000199A2C37DF0>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'))
解决方案
推荐阅读
- spring - 如何更改 Hibernate HQL 查询的输出格式
- autohotkey - 触发«如果最后一个字符是空格并且»如果不是
- tensorflow.js - tensorflow.js 中的 node.js 动态自学问题网络
- database - 将数据导入mongo atlas cluster mongimport时出错
- c# - 处理不可变的一次性对象
- python - 在python中将具有不同字体的字符串转换为纯字符串
- react-native - 在谷歌播放控制台中获取 java.lang.RuntimeException 崩溃和 ANR
- ruby-on-rails - json模式验证不强制类型
- r - 使用 DESeq2 进行带有交互项的差异表达分析
- html - 如何将字母放在侧边栏中?