我有一个允许我通过WebSocket
连接到另一台设备的代码。
它可以完美地在Ubuntu 16.04.2上运行,但是我必须在macOS High Sierra(10.13.5)上对其进行测试。
但是在macOS上有一个错误:
Traceback (most recent call last):
File "/Users/valentindusollier/Desktop/dusollier@dfs/Home/bin/main.py", line 126, in <module>
b.append(Accessory(a.attrib["id"], a.find("name").text, rooms.getRoomByID(a.attrib["rooms"]), a.find("image").text, a.find("address").text))
File "/Users/valentindusollier/Desktop/dusollier@dfs/Home/bin/classes/Accessories.py", line 38, in __init__
self.connect('ws://' + self.addr)
File "/Users/valentindusollier/Desktop/dusollier@dfs/Home/bin/classes/Client.py", line 36, in connect
ws_conn = websocket.WebSocketClientConnection(ioloop.IOLoop.current(), request)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tornado/websocket.py", line 1087, in __init__
scheme, sep, rest = request.url.partition(':')
AttributeError: 'KQueueIOLoop' object has no attribute 'url'
提示: self.connect('ws://' + self.addr)
的调用值类似于ws://192.168.1.102:9001
代码:
from tornado import escape
from tornado import gen
from tornado import httpclient
from tornado import httputil
from tornado import ioloop
from tornado import websocket
import functools
import json
import time
APPLICATION_JSON = 'application/json'
DEFAULT_CONNECT_TIMEOUT = 60
DEFAULT_REQUEST_TIMEOUT = 60
class WSClient():
"""Base for web socket clients.
"""
def __init__(self):
self.connect_timeout = DEFAULT_CONNECT_TIMEOUT
self.request_timeout = DEFAULT_REQUEST_TIMEOUT
def connect(self, url):
"""Connect to the server.
:param str url: server URL.
"""
headers = httputil.HTTPHeaders({'Content-Type': APPLICATION_JSON})
request = httpclient.HTTPRequest(url=url, headers=headers)
#request = httpclient.HTTPRequest(url=url, connect_timeout=self.connect_timeout,request_timeout=self.request_timeout,headers=headers)
ws_conn = websocket.WebSocketClientConnection(ioloop.IOLoop.current(), request)
ws_conn.connect_future.add_done_callback(self._connect_callback)
def send(self, data):
"""Send message to the server
:param str data: message.
"""
if not self._ws_connection:
raise RuntimeError('Web socket connection is closed.')
self._ws_connection.write_message(escape.utf8(json.dumps(data)))
def close(self):
"""Close connection.
"""
if not self._ws_connection:
raise RuntimeError('Web socket connection is already closed.')
self._ws_connection.close()
def _connect_callback(self, future):
if future.exception() is None:
self._ws_connection = future.result()
self._on_connection_success()
self._read_messages()
else:
self._on_connection_error(future.exception())
@gen.coroutine
def _read_messages(self):
while True:
msg = yield self._ws_connection.read_message()
if msg is None:
self._on_connection_close()
break
self._on_message(msg)
def _on_message(self, msg):
"""This is called when new message is available from the server.
:param str msg: server message.
"""
pass
def _on_connection_success(self):
"""This is called on successful connection ot the server.
"""
pass
def _on_connection_close(self):
"""This is called when server closed the connection.
"""
pass
def _on_connection_error(self, exception):
"""This is called in case if connection to the server could
not established.
"""
pass
希望您能帮助我:D