Windows 10 で VSCode を使用して Python 3.10 で websocket-client パッケージを使用しようとしています。Python 3.10.1 を使用して Ubuntu 20.04 VM で実行されているまったく同じコードを取得できます。
これが私のwebsocket-clientバージョンです:
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()