forked from iliis/crossword
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_send_command.py
executable file
·77 lines (50 loc) · 1.38 KB
/
test_send_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/env python3
import json
import socket
HOST = 'localhost'
# set up connection
con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
con.connect( (HOST, 1234) )
print("connected to", con.getpeername())
def send(pkt):
data = json.dumps(pkt)
pkt = "{}\n{}\n".format(len(data), data).encode('ascii')
print("sending raw command: '{}'".format(pkt))
con.sendall(pkt)
print("sent command")
data = con.recv(4096)
print("got reply:")
l, payload = data.decode('utf8').split('\n', 1)
l = int(l)
payload = payload.strip()
if len(payload) != l:
print("WARNING: malformed packet: invalid length! Got", len(payload), "bytes instead of", l)
print(json.loads(payload))
def send_popup():
print("send popup")
pkt = {'command': 'show_popup'}
pkt['title'] = input('Titel:')
pkt['text'] = input('Text:')
buttons = input('Buttons (leave empty for "OK"):').strip()
if len(buttons) > 0:
pkt['buttons'] = buttons.split(',')
send(pkt)
def quit():
print("sending quit command")
send({'command': 'quit'})
def ping():
send({'command': 'ping'})
def reset():
send({'command': 'reset'})
fns = [
send_popup,
quit,
reset,
ping,
]
print("choose a command:")
for i, fn in enumerate(fns):
print(i, fn.__name__)
n = int(input("?"))
if n < len(fns):
fns[n]()