我正在Ubuntu 18.04上使用Python 3.6。
我有一个课程,我正在尝试向其中添加WebSocket服务器。我正在使用此处找到的Python websockets模块: https://github.com/websocket-client/websocket-client
该项目的示例如下所示:
import websocket
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://echo.websocket.org/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
在我的代码中,我在类的构造函数中执行以下操作:
# Configure a command and control websocket
self.commandWebsocket = None
self.commandWebsocketThread = threading.Thread(target=self.configureAndRunCommandChannelWebsocket, args=(commandAndControlPort,))
self.commandWebsocketThread.daemon = True
self.commandWebsocketThread.start()
并按如下所示设置网络套接字:
def onCommandChannelWebsocketMessage(self, ws, message):
self.debugPrint("Websocket Message: %s"%(message))
def onCommandChannelWebsocketError(self, ws, error):
self.debugPrint("Websocket Error: %s"%(error))
def onCommandChannelWebsocketClose(self, ws):
self.debugPrint("Websocket Closed ...")
def onCommandChannelWebsocketOpen(self, ws):
self.debugPrint("Websocket Opened ...")
def run(*args):
for i in range(30000):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
#thread.start_new_thread(run, ())
def configureAndRunCommandChannelWebsocket(self,wsPort):
self.debugPrint("Configuring Command and Control Websocket with: %s ..."%(wsPort))
websocket.enableTrace(True)
self.commandWebsocket = websocket.WebSocketApp("ws://0.0.0.0:%s/"%(wsPort))
self.commandWebsocket.on_message = self.onCommandChannelWebsocketMessage
self.commandWebsocket.on_error = self.onCommandChannelWebsocketError
self.commandWebsocket.on_close = self.onCommandChannelWebsocketClose
self.commandWebsocket.on_open = self.onCommandChannelWebsocketOpen
self.commandWebsocket.run_forever()
但是,我总是收到错误消息:
error from callback <bound method Camera.onCommandChannelWebsocketError of <Cameras.Camera.Camera object at 0x7f34316a58>>: onCommandChannelWebsocketError() missing 1 required positional argument: 'error'
[Camera.py / Camera]: Pipeline: v4l2src device=/dev/video2 ! video/x-raw, width=(int)160, height=(int)120,format=GRAY16_LE ! appsink
File "/usr/local/lib/python3.6/dist-packages/websocket/_app.py", line 343, in _callback
callback(*args)
error from callback <bound method Camera.onCommandChannelWebsocketClose of <Cameras.Camera.Camera object at 0x7f34316a58>>: onCommandChannelWebsocketClose() missing 1 required positional argument: 'ws'
[Camera.py / Camera]: Initializing a Generic Camera
File "/usr/local/lib/python3.6/dist-packages/websocket/_app.py", line 343, in _callback
callback(*args)
我在做什么错,我该如何解决?谢谢!
答案 0 :(得分:2)
将设置更改为以下内容:
def onCommandChannelWebsocketMessage(self, message):
self.debugPrint("Websocket Message: %s"%(message))
def onCommandChannelWebsocketError(self, error):
self.debugPrint("Websocket Error: %s"%(error))
def onCommandChannelWebsocketClose(self):
self.debugPrint("Websocket Closed ...")
def onCommandChannelWebsocketOpen(self):
self.debugPrint("Websocket Opened ...")
def run(*args):
for i in range(30000):
time.sleep(1)
self.send("Hello %d" % i)
time.sleep(1)
self.close()
#thread.start_new_thread(run, ())
def configureAndRunCommandChannelWebsocket(self,wsPort):
self.debugPrint("Configuring Command and Control Websocket with: %s ..."%(wsPort))
websocket.enableTrace(True)
self.commandWebsocket = websocket.WebSocketApp("ws://0.0.0.0:%s/"%(wsPort))
self.commandWebsocket.on_message = self.onCommandChannelWebsocketMessage
self.commandWebsocket.on_error = self.onCommandChannelWebsocketError
self.commandWebsocket.on_close = self.onCommandChannelWebsocketClose
self.commandWebsocket.on_open = self.onCommandChannelWebsocketOpen
self.commandWebsocket.run_forever()