无法建立新连接 Discord.py

Posted

技术标签:

【中文标题】无法建立新连接 Discord.py【英文标题】:Failed to establish a new connection Discord.py 【发布时间】:2019-05-30 17:44:57 【问题描述】:

您好,在我的代码中它破坏了我的请求,我尝试了几次,但 1-2 小时后机器人状态不再改变。

我将这些文件托管在 ftp 服务器中。

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
import math, time
import requests

Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()

@client.event
async def on_ready():

client.loop.create_task(scheduler())
print('project ready')

async def scheduler():
while True:
    # sleep until the next whole second
    now = time.time()
    await asyncio.sleep(math.ceil(now) - now)

    await client.change_presence(game=discord.Game(name='steady', type=3))
    await asyncio.sleep(5)
    await client.change_presence(game=discord.Game(name='ready', type=3))
    await asyncio.sleep(5)
    await client.change_presence(game=discord.Game(name='go', type=3))
    await asyncio.sleep(5)
    await client.change_presence(game=discord.Game(name='Total Used : '+requests.get('http://username.mydomain/project/total_visit/count.txt').text, type=3))
    await asyncio.sleep(10)
    await client.change_presence(game=discord.Game(name='Online : '+requests.get('http://username.mydomain/project/total_online/Live.php').text, type=3))
    await asyncio.sleep(10)
    await client.change_presence(game=discord.Game(name='Total Users : '+requests.get('http://username.mydomain/project/total_users/total.php').text, type=3))
    await asyncio.sleep(10)
    await client.change_presence(game=discord.Game(name='return again', type=3))

    #i tired to kill all request to start clear again.
    client.logout()
    client.close()
    await asyncio.sleep(20)


    client.run("client_token")

从未检索到任务异常 未来:异常=ConnectionError(MaxRetryError(“HTTPConnectionPool(主机='username.mydomain',端口=80):最大重试次数超过了url:/project/total_visit/count.txt(由NewConnectionError引起(':无法建立新的连接:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它',))",),)> 回溯(最近一次通话最后): _new_conn 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py”,第 159 行 (self._dns_host, self.port), self.timeout, **extra_kw) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\connection.py”,第 80 行,在 create_connection 提出错误 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\connection.py”,第 70 行,在 create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] 由于目标机器主动拒绝,无法建立连接

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py”,第 600 行,在 urlopen 分块=分块) _make_request 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py”,第 354 行 conn.request(方法,网址,**httplib_request_kw) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1239 行,在请求中 self._send_request(方法、url、正文、标头、encode_chunked) _send_request 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1285 行 self.endheaders(正文,encode_chunked=encode_chunked) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1234 行,在 endheaders self._send_output(message_body, encode_chunked=encode_chunked) _send_output 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1026 行 自我发送(味精) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 964 行,在发送中 self.connect() 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py”,第 181 行,在连接中 conn = self._new_conn() _new_conn 中的文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py”,第 168 行 self, "建立新连接失败: %s" % e) urllib3.exceptions.NewConnectionError: : 无法建立新连接:[WinError 10061] 由于目标机器主动拒绝,无法建立连接

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py”,第 449 行,发送 超时=超时 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py”,第 638 行,在 urlopen _stacktrace=sys.exc_info()[2]) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\retry.py”,第 398 行,增量 raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='username.mydomain', port=80): Max retries exceeded with url: /project/total_visit/count.txt (由 NewConnectionError(': 无法建立新连接: [ WinError 10061] 由于目标机器主动拒绝,无法建立连接',))

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 调度程序中的文件“C:\Users\Administrator\Desktop\bot.py”,第 33 行 await client.change_presence(game=discord.Game(name='Total Used : '+requests.get('http://username.mydomain/project/total_visit/count.txt').text, type=3)) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py”,第 75 行,在 get 返回请求('get', url, params=params, **kwargs) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\api.py”,第 60 行,在请求中 返回 session.request(method=method, url=url, **kwargs) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py”,第 533 行,在请求中 resp = self.send(prep, **send_kwargs) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py”,第 646 行,发送 r = adapter.send(request, **kwargs) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py”,第 516 行,发送 引发 ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPConnectionPool(host='username.mydomain', port=80): Max retries exceeded with url: /project/total_visit/count.txt (由 NewConnectionError(': 无法建立新连接: [ WinError 10061] 由于目标机器主动拒绝,无法建立连接',))

【问题讨论】:

【参考方案1】:

肯定你必须得到错误,你做了很多get.requests。

要解决它,请尝试(关闭请求)/(重新启动 Bot [后台命令])

嗯,我还不够好,但你可以试试我的步骤

1) 添加完整(urllib3 库)

import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
import math, time
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import urllib3
import urllib3.contrib.pyopenssl
import certifi

2) add (Backgroundworker) 命令[每次都会重启你的get.request。]

Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()

urllib3.contrib.pyopenssl.inject_into_urllib3()

@client.event
async def on_ready():
print('Project Runing..!')


async def task():
await client.wait_until_ready()
while True:

   await client.change_presence(game=discord.Game(name='Loading..!', type=3))

   print('Get_Url-Refresh')
   await asyncio.sleep(1)
   print('Total_Used')
   one = requests.get('http://username.mydomain/project/total_visit/count.txt', timeout=5)
   print('Total_Online')
   two = requests.get('http://username.mydomain/project/total_online/Live.php', timeout=5)
   print('Total_Users')
   three = requests.get('http://username.mydomain/project/total_users/total.php', timeout=5)
   await asyncio.sleep(1)
   print('Refresh Request')

   await asyncio.sleep(10)



   print('Running')
   await client.change_presence(game=discord.Game(name='steady', type=3))
   await asyncio.sleep(5)
   await client.change_presence(game=discord.Game(name='ready', type=3))
   await asyncio.sleep(5)
   await client.change_presence(game=discord.Game(name='go', type=3))
   await asyncio.sleep(5)
   print('Total Used..!')
   await client.change_presence(game=discord.Game(name='Total Used : ' + one.text, type=3))
   await asyncio.sleep(5)
   print('Online..!')
   await client.change_presence(game=discord.Game(name='Online : ' + two.text, type=1))
   await asyncio.sleep(5)
   print('Total Users..!')
   await client.change_presence(game=discord.Game(name='Total Users : ' + three.text, type=3))
   await asyncio.sleep(5)
   print('Refresh..!')

   def handle_exit():
print("Handling")
client.loop.run_until_complete(client.logout())
for t in asyncio.Task.all_tasks(loop=client.loop):
    if t.done():
        t.exception()
        continue
    t.cancel()
    try:
        client.loop.run_until_complete(asyncio.wait_for(t, 5, loop=client.loop))
        t.exception()
    except asyncio.InvalidStateError:
        pass
    except asyncio.TimeoutError:
        pass
    except asyncio.CancelledError:
        pass


   while True:
   @client.event
   async def on_message(m):
    if m.content == 'die':
        print("Terminating")
        raise SystemExit

client.loop.create_task(task())
try:
    client.loop.run_until_complete(client.start('You_Token_Here'))
except SystemExit:
    handle_exit()
except KeyboardInterrupt:
    handle_exit()
    client.loop.close()
    print("Program ended")
    break

print("Bot restarting")
client = discord.Client(loop=client.loop)

归功于 Dev I.A(Gamer's Control)。

【讨论】:

我想我明白你的意思了,很酷。谢谢你的帮助。我会测试它,

以上是关于无法建立新连接 Discord.py的主要内容,如果未能解决你的问题,请参考以下文章

远程桌面连接已断开 客户端无法连接。您已连接到这台计算机的控制台。无法建立新的控制台会话。

什么可以延迟与 NetTcpBinding 建立新连接?

离线使用数据库,然后在与 iPhone 建立新连接时更新

当 SSL SYSCALL 错误和 CONN_MAX_AGE > 0 时强制建立新的数据库连接

使用MySQL Workbench建立数据库,建立新的表,向表中添加数据

gt730显卡hdmi不输出原因