28 lines
889 B
Python
28 lines
889 B
Python
import ssl
|
||
import websocket
|
||
|
||
URL = "ws://192.168.1.8:8084/ws" # 注意走 443,不要再连 8080 了
|
||
# 如果你的 WS 路径是 /ws/,就写成上面这样;若是别的路径自己改
|
||
|
||
def on_message(ws, msg): print("收到:", msg)
|
||
def on_error(ws, err): print("错误:", err)
|
||
def on_close(ws, code, reason): print("关闭:", code, reason)
|
||
def on_open(ws):
|
||
print("连接成功")
|
||
ws.send("hello server mac")
|
||
|
||
if __name__ == "__main__":
|
||
websocket.enableTrace(True)
|
||
ws = websocket.WebSocketApp(
|
||
URL,
|
||
on_open=on_open,
|
||
on_message=on_message,
|
||
on_error=on_error,
|
||
on_close=on_close,
|
||
# header=["Origin: https://192.168.1.3"] # 如后端不校验 Origin 可删
|
||
header=[] # 如后端不校验 Origin 可删
|
||
)
|
||
ws.run_forever(sslopt={
|
||
"cert_reqs": ssl.CERT_NONE,
|
||
"check_hostname": False,
|
||
}) |