You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 07:42:47