py-test/test_websocket.py

28 lines
891 B
Python
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import ssl
import websocket
URL = "ws://192.168.1.41:9516/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,
})