如何修复 Python WebSocket TypeError: __init__() missing 3 required positional arguments: 'environ', 'socket', and 'rfile'

问题:

你正在尝试在 Python 中连接 WebSocket:

example-4.py
ws = websocket.WebSocket()

但你看到类似这样的错误消息

example-3.txt
TypeError                                 Traceback (most recent call last)
<ipython-input-3-5108525d0b43> in <module>
      1 import websocket
----> 2 ws = websocket.WebSocket()
      3 ws.connect("ws://192.168.1.211/ws")
      4 while True:
      5     result = ws.recv()

TypeError: __init__() missing 3 required positional arguments: 'environ', 'socket', and 'rfile'

解决方案

你安装了错误的库!你需要使用 websocket-client 而不是 websocket

首先,卸载 websocket

example-2.sh
pip uninstall websocket

现在安装 websocket-client:

example-1.py
pip install websocket-client

现在再试一次 - 你的代码现在应该可以工作了。


Check out similar posts by category: Python