Python3 Websocket Callback 无法分配类成员函数

标签 python linux python-3.x websocket callback

我在 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()

然后像这样设置 websocket:

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)

我做错了什么,我该如何解决?

最佳答案

将您的设置更改为以下内容:

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()

关于Python3 Websocket Callback 无法分配类成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55955971/

相关文章:

python - 使用 "konsole"命令运行 python 脚本

python - Pandas str.contains 超过一个列表?

python - 在 Django Admin 中添加指向 Actions 的链接

python - 如何将列表转换为 python 中的计数器对象

python - else : after a try/except clause有什么用

php - 我怎样才能在 PHP 邮件程序中延迟发送大量电子邮件

linux - 如何替换括号之间的换行符

linux - 两个目标文件中 md5sums 的差异

python - 从一列复制字符串的一部分并将其放入新的 Pandas 列中

Python - 需要像对象这样的字节,而不是 str