Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/scripts/websocket-test.py
3657 views
1
# Initially by Nemoumbra, extended to support more parameters and receive responses by ChatGPT.
2
# Example usage from the root:
3
# > python scripts\websocket-test.py 56244 gpu.stats.get
4
# NOTE: For some reason fails to connect from WSL, this should be investigated.
5
6
import sys
7
import time
8
from websocket import WebSocket
9
from json import dumps
10
11
12
def main():
13
if len(sys.argv) not in (3, 4):
14
print(f"Usage: {sys.argv[0]} <port> <cmd> [wait_secs]")
15
print("Example commands: gpu.stats.get game.reset game.status (there are more)")
16
print("Default wait time: 2 seconds")
17
sys.exit(1)
18
19
# Validate port
20
try:
21
port = int(sys.argv[1])
22
if not (1 <= port <= 65535):
23
raise ValueError("Port must be between 1 and 65535")
24
except ValueError as e:
25
print(f"Invalid port: {e}")
26
sys.exit(1)
27
28
cmd = sys.argv[2]
29
30
# Parse wait time (default = 2)
31
try:
32
wait_secs = int(sys.argv[3]) if len(sys.argv) == 4 else 2
33
if wait_secs < 0:
34
raise ValueError("Wait time must be non-negative")
35
except ValueError as e:
36
print(f"Invalid wait_secs: {e}")
37
sys.exit(1)
38
39
host = "127.0.0.1"
40
uri = f"ws://{host}:{port}/debugger"
41
42
ws = WebSocket()
43
try:
44
ws.connect(uri)
45
request = {"event": cmd}
46
ws.send(dumps(request))
47
print(f"Sent {cmd} event to {uri}, listening for {wait_secs} second(s)...")
48
49
ws.settimeout(wait_secs)
50
start = time.time()
51
while True:
52
try:
53
response = ws.recv()
54
print("Received response:", response)
55
except Exception:
56
# Stop when timeout occurs or no more messages
57
break
58
if time.time() - start > wait_secs:
59
break
60
61
except Exception as e:
62
print(f"Connection failed: {e}")
63
finally:
64
ws.close()
65
66
67
if __name__ == "__main__":
68
main()
69