Python客户端连接WebSocket服务器报错[Errno 11001] getaddrinfo failed
Python WebSocket连接报错[Errno 11001] getaddrinfo failed的问题与解决
我尝试使用Python通过WebSocket连接Web服务器,但始终无法成功,持续收到错误:[Errno 11001] getaddrinfo failed
客户端代码
import asyncio import websockets from websockets.sync.client import connect def connection(): with connect("ws://http://<ip address>") as ws: ws.send("Hello world!") message = ws.recv() print(f"Received: {message}") if __name__ == "__main__": connection()
报错信息
line 14, in <module> connection() line 7, in connection with connect("ws://http://<ip address>/") as ws: line 247, in connect sock = socket.create_connection( line 824, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): line 955, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed
解决方法
已解决该问题,原因是忘记在Windows的hosts文件(路径为C:\Windows\System32\drivers\etc\hosts)中添加IP地址与主机名的映射,添加以下内容并保存后,问题即可解决:
<ip address> <hostname>
内容的提问来源于stack exchange,提问作者Learning Student
相关产品推荐
相关产品推荐

