首页 > 解决方案 > 无法建立新连接 Discord.py

问题描述

您好,在我的代码中它破坏了我的请求,我尝试了几次,但 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")

任务异常永远不会被检索到未来: exception=ConnectionError(MaxRetryError("HTTPConnectionPool(host='username.mydomain', port=80): Max retries exceeded with url: /project/total_visit/count.txt (Caused by NewConnectionError(':无法建立新连接:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它',))",),)> Traceback(最近一次调用最后):文件“C:\Users\Administrator\ AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py",第 159 行,在 _new_conn (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] 否可以建立连接,因为目标机器主动拒绝它

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

回溯(最后一次调用):文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py”,第 600 行,在 urlopen chunked=chunked ) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py”,第 354 行,在 _make_request conn.request(方法,url,** httplib_request_kw) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1239 行,在请求 self._send_request(method, url, body, headers, encode_chunked ) 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1285 行,_send_request self.endheaders(body, 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) 文件“C:\Users\Administrator\ AppData\Local\Programs\Python\Python36-32\lib\http\client.py”,第 1026 行,在 _send_output self.send(msg) 文件“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() 文件“C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py ",第 168 行,在 _new_conn 中,"无法建立新连接:%s" % e) urllib3.exceptions.NewConnectionError: : 无法建立新连接: [WinError 10061] 由于目标机器主动拒绝,无法建立连接

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

回溯(最后一次调用):文件“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 行,增量提高 MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='username.mydomain', port=80): 最大重试次数超过 url: /project/total_visit/count.txt (由 NewConnectionError(': 无法建立一个新的连接:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它',))

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

回溯(最近一次调用):文件“C:\Users\Administrator\Desktop\bot.py”,第 33 行,在调度程序中等待 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 行,获取返回request('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):最大重试次数超过 url:/project/total_visit/count.txt(由 NewConnectionError('引起:无法建立新连接:[WinError 10061] 无法连接)因为目标机器主动拒绝它而被制造',))无法建立新连接:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它',))无法建立新连接:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它',))

标签: python-3.xpython-requestspython-multithreadingdiscord.py

解决方案


绝对你必须得到错误,你做了很多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)添加(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 IA(Gamer's Control)。


推荐阅读