py-test/test_websocket.py

28 lines
1.1 KiB
Python

import ssl
import websocket
URL = "ws://192.168.1.41:9516/ws/"
# 如果你的 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")
if __name__ == "__main__":
websocket.enableTrace(True)
token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZW5hbnRfcm9sZV9rZXkiOiJqeHpqIiwidXNlcl9pZCI6IjAwMjk0MjYwMTY5OTc1NzU0NDMiLCJ1c2VyX25hbWUiOiJkZW1vIiwic2NvcGUiOlsic2VydmVyIl0sImV4cCI6MTc1NDY0MjM0MCwianRpIjoiRUtlaURqSEtsVHc4aG5jdUNpOXRmMjlZMTVBIiwiY2xpZW50X2lkIjoiNTczOTVkMmJjNjY4NDk2YzljNTdkOGYyYjE5YmQ1MTYifQ.2O4W-cVOjpQDrFwHKXrKH9kfZHoghfZ0buKvY1eGbMM'
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 可删
)
ws.run_forever(sslopt={
"cert_reqs": ssl.CERT_NONE,
"check_hostname": False,
})