如何修复电报机器人中的连接错误?

问题描述

我开始写一个电报机器人,并使用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,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',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连接电报。

我在echo bot中遇到另一个连接错误。我使用了电报机器人文档代码。我收到此错误

raise MaxRetryError(_pool,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,in <module>
    contents = requests.get('https://google').json()
  File "D:\workbot\venv\lib\site-packages\requests\api.py",request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='google',or established connection Failed because connected host has Failed to respond'))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)