-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_control-client.py
60 lines (53 loc) · 1.33 KB
/
remote_control-client.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
from argparse import ArgumentParser
import os
import signal
import socket
from time import sleep
from utils.controller import *
def signal_handler(sig, frame):
print("Closing...")
os.system('stty echo')
exit(0)
signal.signal(signal.SIGINT, signal_handler)
HOST = "127.0.0.1"
PORT = 1234
parser = ArgumentParser()
parser.add_argument(
'host',
metavar='HOST',
type=str,
default=HOST,
nargs='?',
help='the host of the server'
)
parser.add_argument(
'port',
metavar='PORT',
type=int,
default=PORT,
nargs='?',
help='the port the server should listen for'
)
args = parser.parse_args()
HOST = args.host
PORT = args.port
os.system('stty -echo')
controller = get_controller()
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as so:
try:
print(f'Connecting to {HOST}:{PORT}...')
so.connect((HOST, PORT))
except ConnectionRefusedError:
print('Retrying in 3 seconds...')
sleep(3)
continue
else:
print(f'Connected to {HOST}:{PORT}!')
while True:
try:
c_input = controller.read()
so.sendall(c_input.value.to_bytes(1, 'little'))
print(f'Sent {c_input}')
except OSError:
controller = get_controller()