-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTCPThreadedServer.py
185 lines (153 loc) · 6.05 KB
/
TCPThreadedServer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from datetime import datetime
from json import loads, dumps
import socket
from threading import Thread
class TCPThreadedServer(Thread):
def __init__(
self,
host,
port,
timeout=60,
decode_json = True,
on_connected_callback=None,
on_receive_callback=None,
on_disconnected_callback=None,
debug=False,
debug_data=False
):
self.host = host
self.port = port
self.timeout = timeout
self.decode_json = decode_json
self.on_connected_callback = on_connected_callback
self.on_receive_callback = on_receive_callback
self.on_disconnected_callback = on_disconnected_callback
self.debug = debug
self.debug_data = debug_data
self.clients = []
Thread.__init__(self)
# run by the Thread object
def run(self):
if self.debug:
print(datetime.now(), 'SERVER Starting...', '\n')
self.listen()
def listen(self):
# create an instance of socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# bind the socket to its host and port
self.sock.bind((self.host, self.port))
if self.debug:
print(datetime.now(), 'SERVER Socket Bound', self.host, self.port, '\n')
# start listening for a client
self.sock.listen(5)
if self.debug:
print(datetime.now(), 'SERVER Listening...', '\n')
while True:
# get the client object and address
client, address = self.sock.accept()
# add client to list
self.clients.append(client)
# set a timeout
client.settimeout(self.timeout)
if self.debug:
print(datetime.now(), 'CLIENT Connected', client, '\n')
if self.on_connected_callback:
self.on_connected_callback(client, address)
# start a thread to listen to the client
Thread(
target=self.listen_to_client,
args=(client, address, self.decode_json, self.on_receive_callback, self.on_disconnected_callback)
).start()
def listen_to_client(self, client, address, decode_json, on_receive_callback, on_disconnected_callback):
# set a buffer size ( could be 2048 or 4096 / power of 2 )
size = 1024*1024
while True:
try:
d = client.recv(size)
if d:
#messages = d.split(b'\n\r')
messages = d.split(b'\0')
for data in messages:
if data:
data = data.decode('utf-8')
if self.debug:
print(datetime.now(), 'CLIENT Data Received', address)
if not self.debug_data:
print('\n')
if decode_json:
try:
data = loads(data)
except Exception as e:
if self.debug:
print(datetime.now(), 'CLIENT Json Failed:', data, '\n', e, '\n')
break
if self.debug_data:
print(data, '\n')
if on_receive_callback:
try:
on_receive_callback(client, address, data)
except Exception as e:
if self.debug:
print(datetime.now(), 'CLIENT Receive Callback Failed:', data, '\n', e, '\n')
else:
raise ValueError('CLIENT Disconnected')
except Exception as e:
if self.debug:
print(datetime.now(), e, client, '\n')
client.close()
client_index = self.clients.index(client)
self.clients.pop(client_index)
if on_disconnected_callback:
try:
on_disconnected_callback(client, address)
except Exception as e:
print('on_close_callback failed\n', e, '\n')
return False
def send_all(self, cmd, data):
for client in self.clients:
# send each client a message
res = {
'cmd': cmd,
'data': data
}
response = dumps(res, default=str)
# add new line chr for TD
response += '\n'
client.send(response.encode('utf-8'))
def example_on_receive_callback(client, address, data):
#print('data received', client, address, data)
# send a response back to the client
res = data
response = dumps(res, default=str)
# add new line chr for TD
response += '\n'
#print(response)
client.send(response.encode('utf-8'))
return
def example_on_connected_callback(client, address):
#print('client connected', client, address)
# send the client a connection message
res = {
'cmd': 'connected',
}
response = dumps(res, default=str)
# add new line chr for TD
response += '\n'
client.send(response.encode('utf-8'))
return
def example_on_disconnected_callback(client, address):
#print('client disconnected', client, address)
return
if __name__ == "__main__":
TCPThreadedServer(
'127.0.0.1',
8008,
timeout=86400,
decode_json=True,
on_connected_callback=example_on_connected_callback,
on_receive_callback=example_on_receive_callback,
on_disconnected_callback=example_on_disconnected_callback,
debug=True,
debug_data=True
).start()