我正在尝试编写一个websocket,它连接到在localhost上运行的服务,但它正在抛出错误
>>> from websocket import create_connection
>>> ws = create_connection("ws://127.0.0.1", http_proxy_port="2974", http_proxy_host="quividicontent")
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
ws = create_connection("ws://127.0.0.1", http_proxy_port="2974", http_proxy_host="quividicontent")
File "C:\Python27\lib\websocket\_core.py", line 487, in create_connection
websock.connect(url, **options)
File "C:\Python27\lib\websocket\_core.py", line 211, in connect
options.pop('socket', None))
File "C:\Python27\lib\websocket\_http.py", line 64, in connect
hostname, port, is_secure, proxy)
File "C:\Python27\lib\websocket\_http.py", line 97, in _get_addrinfo_list
addrinfo_list = socket.getaddrinfo(phost, pport, 0, 0, socket.SOL_TCP)
gaierror: [Errno 11001] getaddrinfo failed
>>> ws = create_connection("ws://127.0.0.1", http_proxy_port="2974")
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
ws = create_connection("ws://127.0.0.1", http_proxy_port="2974")
File "C:\Python27\lib\websocket\_core.py", line 487, in create_connection
websock.connect(url, **options)
File "C:\Python27\lib\websocket\_core.py", line 214, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "C:\Python27\lib\websocket\_handshake.py", line 65, in handshake
status, resp = _get_resp_headers(sock)
File "C:\Python27\lib\websocket\_handshake.py", line 122, in _get_resp_headers
raise WebSocketBadStatusException("Handshake status %d", status)
WebSocketBadStatusException: Handshake status 200
>>> import socket
>>> socket.getaddrinfo('localhost', 2974)
[(23, 0, 0, '', ('::1', 2974, 0, 0)), (2, 0, 0, '', ('127.0.0.1', 2974))]
我确信端口是开放式的,正在使用套接字以及onOpen
onMessage
函数的更复杂示例。
答案 0 :(得分:0)
出现此问题是因为您需要为websocket指定握手协议。
来自wikipedia:
要建立WebSocket连接,客户端会发送WebSocket 握手请求,服务器返回WebSocket握手 响应,如下例所示。
在此握手过程中,服务器和客户端讨论应该使用哪些协议,如果没有指定协议,这可能会导致错误(并非总是如此)。
从python-websocket doc中,指定一个可以完成的协议 :
ws = websocket.create_connection("ws://exapmle.com/websocket", subprotocols=["binary", "base64"])
在您的特定情况下,subprotocols
应为['quividicontent']