a1468884069
New Member
This is my code
import asyncio
import websockets
import json
from encrypy_pw import encrypt_str
import logging
from config import password
obs_websocket_uri = "ws://192.168.0.103:4455"
logging.basicConfig(level=logging.DEBUG)
async def send_websocket(uri, password):
# 连接到obs-websocket服务器
async with websockets.connect(uri, subprotocols=["obswebsocket.json"]) as websocket:
hello_message = {
"op": 0,
"d": {
}
}
# 发送请求
await websocket.send(json.dumps(hello_message))
hello_response = await websocket.recv()
hello_data = json.loads(hello_response)
print("Response:", hello_data)
challenge_str = hello_data["d"]["authentication"]["challenge"]
salt_str = hello_data["d"]["authentication"]["salt"]
auth_str = encrypt_str(challenge_str, salt_str, password)
# obs_Version = hello_data["d"]["'obsWebSocketVersion'"]
rpc_Version = hello_data["d"]['rpcVersion']
auth_request = {
"op": 1,
"d": {
"rpcVersion": rpc_Version,
"authentication": auth_str,
"eventSubscriptions": 33
}
}
try:
await websocket.send(json.dumps(auth_request))
auth_response = await websocket.recv()
auth_data = json.loads(auth_response)
print("Response:", auth_data)
except websockets.exceptions.WebSocketException as wse: # 捕获 WebSocket 相关的异常
print(f"WebSocket error: {wse}")
except json.JSONDecodeError as jde: # 捕获 JSON 解析错误
print(f"JSON decode error: {jde}")
except Exception as e: # 捕获所有其他异常
print(f"An unexpected error occurred: {e}")
asyncio.run(send_websocket(obs_websocket_uri, password))
Output from the console
F:\PycharmProjects\pythonProject\.venv\Scripts\python.exe F:\PycharmProjects\pythonProject\day0908\OCR_dou\obs_client.py
DEBUG:asyncio:Using proactor: IocpProactor
Response: {'d': {'authentication': {'challenge': '+oR5aO/V0EiKA/DFdKw8bQkzPQrPWeBPVdmVLvF+1FI=', 'salt': 'R4uv4zJbPzBnLC7gncZjoalaA/lni58MN+bMNfQxGFo='}, 'obsWebSocketVersion': '5.5.2', 'rpcVersion': 1}, 'op': 0}
WebSocket error: received 4007 (private use) You attempted to send a non-Identify message while not identified.; then sent 4007 (private use) You attempted to send a non-Identify message while not identified.
DEBUG:websockets.client:= connection is CONNECTING
DEBUG:websockets.client:> GET / HTTP/1.1
DEBUG:websockets.client:> Host: 192.168.0.103:4455
DEBUG:websockets.client:> Upgrade: websocket
DEBUG:websockets.client:> Connection: Upgrade
DEBUG:websockets.client:> Sec-WebSocket-Key: Lo05HN2J0faUWFlIdG3uKQ==
DEBUG:websockets.client:> Sec-WebSocket-Version: 13
DEBUG:websockets.client:> Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
DEBUG:websockets.client:> Sec-WebSocket-Protocol: obswebsocket.json
DEBUG:websockets.client:> User-Agent: Python/3.12 websockets/12.0
DEBUG:websockets.client:< HTTP/1.1 101 Switching Protocols
DEBUG:websockets.client:< Connection: Upgrade
DEBUG:websockets.client:< Sec-WebSocket-Accept: AfBfk405fBVgEnB2IDYsNUJIqaU=
DEBUG:websockets.client:< Sec-WebSocket-Protocol: obswebsocket.json
DEBUG:websockets.client:< Server: WebSocket++/0.8.2
DEBUG:websockets.client:< Upgrade: websocket
DEBUG:websockets.client:= connection is OPEN
DEBUG:websockets.client:> TEXT '{"op": 0, "d": {}}' [18 bytes]
DEBUG:websockets.client:< TEXT '{"d":{"authentication":{"challenge":"+oR5aO/V0E..."rpcVersion":1},"op":0}' [191 bytes]
DEBUG:websockets.client:> TEXT '{"op": 1, "d": {"rpcVersion": 1, "authenticatio...entSubscriptions": 33}}' [125 bytes]
DEBUG:websockets.client:< CLOSE 4007 (private use) You attempted to send a non-I...ge while not identified. [68 bytes]
DEBUG:websockets.client:= connection is CLOSING
DEBUG:websockets.client:> CLOSE 4007 (private use) You attempted to send a non-I...ge while not identified. [68 bytes]
DEBUG:websockets.client:= connection is CLOSED
But obs told me the identity was identified and connected
import asyncio
import websockets
import json
from encrypy_pw import encrypt_str
import logging
from config import password
obs_websocket_uri = "ws://192.168.0.103:4455"
logging.basicConfig(level=logging.DEBUG)
async def send_websocket(uri, password):
# 连接到obs-websocket服务器
async with websockets.connect(uri, subprotocols=["obswebsocket.json"]) as websocket:
hello_message = {
"op": 0,
"d": {
}
}
# 发送请求
await websocket.send(json.dumps(hello_message))
hello_response = await websocket.recv()
hello_data = json.loads(hello_response)
print("Response:", hello_data)
challenge_str = hello_data["d"]["authentication"]["challenge"]
salt_str = hello_data["d"]["authentication"]["salt"]
auth_str = encrypt_str(challenge_str, salt_str, password)
# obs_Version = hello_data["d"]["'obsWebSocketVersion'"]
rpc_Version = hello_data["d"]['rpcVersion']
auth_request = {
"op": 1,
"d": {
"rpcVersion": rpc_Version,
"authentication": auth_str,
"eventSubscriptions": 33
}
}
try:
await websocket.send(json.dumps(auth_request))
auth_response = await websocket.recv()
auth_data = json.loads(auth_response)
print("Response:", auth_data)
except websockets.exceptions.WebSocketException as wse: # 捕获 WebSocket 相关的异常
print(f"WebSocket error: {wse}")
except json.JSONDecodeError as jde: # 捕获 JSON 解析错误
print(f"JSON decode error: {jde}")
except Exception as e: # 捕获所有其他异常
print(f"An unexpected error occurred: {e}")
asyncio.run(send_websocket(obs_websocket_uri, password))
Output from the console
F:\PycharmProjects\pythonProject\.venv\Scripts\python.exe F:\PycharmProjects\pythonProject\day0908\OCR_dou\obs_client.py
DEBUG:asyncio:Using proactor: IocpProactor
Response: {'d': {'authentication': {'challenge': '+oR5aO/V0EiKA/DFdKw8bQkzPQrPWeBPVdmVLvF+1FI=', 'salt': 'R4uv4zJbPzBnLC7gncZjoalaA/lni58MN+bMNfQxGFo='}, 'obsWebSocketVersion': '5.5.2', 'rpcVersion': 1}, 'op': 0}
WebSocket error: received 4007 (private use) You attempted to send a non-Identify message while not identified.; then sent 4007 (private use) You attempted to send a non-Identify message while not identified.
DEBUG:websockets.client:= connection is CONNECTING
DEBUG:websockets.client:> GET / HTTP/1.1
DEBUG:websockets.client:> Host: 192.168.0.103:4455
DEBUG:websockets.client:> Upgrade: websocket
DEBUG:websockets.client:> Connection: Upgrade
DEBUG:websockets.client:> Sec-WebSocket-Key: Lo05HN2J0faUWFlIdG3uKQ==
DEBUG:websockets.client:> Sec-WebSocket-Version: 13
DEBUG:websockets.client:> Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
DEBUG:websockets.client:> Sec-WebSocket-Protocol: obswebsocket.json
DEBUG:websockets.client:> User-Agent: Python/3.12 websockets/12.0
DEBUG:websockets.client:< HTTP/1.1 101 Switching Protocols
DEBUG:websockets.client:< Connection: Upgrade
DEBUG:websockets.client:< Sec-WebSocket-Accept: AfBfk405fBVgEnB2IDYsNUJIqaU=
DEBUG:websockets.client:< Sec-WebSocket-Protocol: obswebsocket.json
DEBUG:websockets.client:< Server: WebSocket++/0.8.2
DEBUG:websockets.client:< Upgrade: websocket
DEBUG:websockets.client:= connection is OPEN
DEBUG:websockets.client:> TEXT '{"op": 0, "d": {}}' [18 bytes]
DEBUG:websockets.client:< TEXT '{"d":{"authentication":{"challenge":"+oR5aO/V0E..."rpcVersion":1},"op":0}' [191 bytes]
DEBUG:websockets.client:> TEXT '{"op": 1, "d": {"rpcVersion": 1, "authenticatio...entSubscriptions": 33}}' [125 bytes]
DEBUG:websockets.client:< CLOSE 4007 (private use) You attempted to send a non-I...ge while not identified. [68 bytes]
DEBUG:websockets.client:= connection is CLOSING
DEBUG:websockets.client:> CLOSE 4007 (private use) You attempted to send a non-I...ge while not identified. [68 bytes]
DEBUG:websockets.client:= connection is CLOSED
But obs told me the identity was identified and connected