-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx0netcontrol.py
68 lines (54 loc) · 1.96 KB
/
x0netcontrol.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
##
## imports
##
import socket
import select
import json
##
## Code
##
class x0NetControl:
"""Receive commands from the network"""
def __init__(self, useLog, port):
self.port = port
self.log = useLog
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind(('',self.port))
self.s.listen()
self.readList = [self.s]
def action(self):
results = []
readable, _writable, errored = select.select(self.readList, [], self.readList, 0)
# self.log.debug(f'readabout {readable}')
# self.log.debug(f'errored {errored}')
for sock in readable:
try:
if sock is self.s:
# new connection
client_socket, address = self.s.accept()
self.readList.append(client_socket)
self.log.debug(f'Connection from {address}')
else:
data = sock.recv(1024)
if data == b'':
# socket is closed
self.log.debug(f'{sock} needs to be closed because empty received')
sock.close()
self.readList.remove(sock)
self.log.debug(f'{sock} Received {data}')
try:
if data != b'':
j = json.loads(data)
results.append(j)
except Exception as ex:
self.log.error(f'{sock} Exception {ex} Could not parse {data}')
raise
except Exception as ex:
self.log.error(f'{sock} Exception {ex} Could not parse {data}')
sock.close()
self.readList.remove(sock)
for sock in errored:
sock.close()
self.readList.remove(sock)
return results