如何解决尝试连接到网站返回“ ConnectionResetError:[WinError 64]指定的网络名称不再可用”
我正在尝试使用原始套接字连接到Discord API。为了提高性能和并发性,我决定使用asyncio
。但是,与套接字模块本身相比,它的协作性要差得多。 sock_connect
无法输入网址的网址,因此我不得不查找Discord的IP(我什至不肯定是正确的)。
import socket
import asyncio
import ssl
loop = asyncio.get_event_loop()
HOST = "162.159.138.232" # Previously was `discord.com`,but that raised...
# `OSError: [WinError 10022] An invalid argument was supplied` in `sock_connect`
PORT = 443 # HTTPS port
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s_sock = context.wrap_socket(s,server_hostname=HOST)
s_sock.setblocking(False)
async def make_connection():
await loop.sock_connect(s_sock,(HOST,PORT))
await loop.sock_sendall(s_sock,b'''GET /api/v6/users/@me/guilds HTTP/1.0\r
Host: discord.com\r
User-Agent: PyDisc ($www.hi.com,$0.0.1)\r
Authorization: Bot tokentokentoken\r
\r\n''')
f = await loop.sock_recv(s_sock,4096)
print(f)
loop.run_until_complete(make_connection())
我的代码现在仅挂起几秒钟,然后以该回溯作为响应:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\windows_events.py",line 453,in finish_recv
return ov.getresult()
OSError: [WinError 64] The specified network name is no longer available
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "c:/Users/User/Desktop/VS Code/client.py",line 26,in <module>
loop.run_until_complete(make_connection())
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py",line 616,in run_until_complete
return future.result()
File "c:/Users/User/Desktop/VS Code/client.py",line 24,in make_connection
f = await loop.sock_recv(s_sock,4096)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\proactor_events.py",line 693,in sock_recv
return await self._proactor.recv(sock,n)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\windows_events.py",line 808,in _poll
value = callback(transferred,key,ov)
File "C:\Users\User\AppData\Local\Programs\Python\Python38-32\lib\asyncio\windows_events.py",line 457,in finish_recv
raise ConnectionResetError(*exc.args)
ConnectionResetError: [WinError 64] The specified network name is no longer available
我已经通过本地生成TCP
服务器(使用socketserver
模块)来测试了此代码,并且它按预期工作。我为此使用了本地IP地址和端口9999。还值得注意的是,当我不使用asyncio
时,此方法有效。
如果您怀疑我为不和谐网站使用的IP地址错误,请在此处找到它(从命令提示符处):
C:\Users\User>ping discord.com
Pinging discord.com [162.159.138.232] with 32 bytes of data:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。