0

我正在尝试在 Windows 10 上使用带有 VSCode 的 Python 3.10 中的 websocket-client 包。我能够在带有 Python 3.10.1 的 Ubuntu 20.04 VM 上运行完全相同的代码

这是我的 websocket 客户端版本:

Name: websocket-client
Version: 1.2.3
Summary: WebSocket client for Python with low level API options
Home-page: https://github.com/websocket-client/websocket-client.git
Author: liris
Author-email: liris.pp@gmail.com
License: Apache-2.0
Location: c:\users\dazk2\appdata\local\programs\python\python310\lib\site-packages
Requires:
Required-by:

我得到错误:

Traceback (most recent call last):
  File "c:\...\websocket.py", line 32, in <module>
    ws = websocket.WebSocketApp("ws://localhost:8765",
AttributeError: module 'websocket' has no attribute 'WebSocketApp'. Did you mean: 'websocket'?

这是代码:(这不是我正在处理的,但我尝试了示例代码,但它仍然没有工作)

import websocket
import _thread as thread
import time

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, a, b):
    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://localhost:8765",
                                 on_message = on_message,
                                 on_error = on_error,
                                 on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()    
4

0 回答 0