关于python 3.x:无法建立新的连接Discord.py

Failed to establish a new connection Discord.py

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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")

Task exception was never retrieved
future: exception=ConnectionError(MaxRetryError("HTTPConnectionPool(host='username.mydomain', port=80): Max retries exceeded with url: /project/total_visit/count.txt (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))",),)>
Traceback (most recent call last):
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
(self._dns_host, self.port), self.timeout, **extra_kw)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection
raise err
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request
conn.request(method, url, **httplib_request_kw)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1239, in request
self._send_request(method, url, body, headers, encode_chunked)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 1026, in _send_output
self.send(msg)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\http\client.py", line 964, in send
self.connect()
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 181, in connect
conn = self._new_conn()
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 168, in _new_conn
self,"Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages
equests\adapters.py", line 449, in send
timeout=timeout
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util
etry.py", line 398, in increment
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 (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File"C:\Users\Administrator\Desktop\bot.py", line 33, in scheduler
await client.change_presence(game=discord.Game(name='Total Used : '+requests.get('http://username.mydomain/project/total_visit/count.txt').text, type=3))
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages
equests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages
equests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages
equests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages
equests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File"C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\site-packages
equests\adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='username.mydomain', port=80): Max retries exceeded with url: /project/total_visit/count.txt (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))


definitely you must to get error,, you made many get.requests.

to slove it, try (Close request) / (restart Bot [background Command])

嗯,我不够好,但你可以试试我的台阶

1)添加完整(URLLIB3库)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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。]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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)

Credits to Dev I.A (Gamer's Control).