-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCP_jsonServer.py
More file actions
48 lines (42 loc) · 1.58 KB
/
TCP_jsonServer.py
File metadata and controls
48 lines (42 loc) · 1.58 KB
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
#!/usr/bin/env python3
#KILDE: https://gist.github.com/ferstar/c112f6aeb9dd47727ac6
import socket
import socketserver, subprocess, sys
from ipaddress import IPv4Address
from threading import Thread
from pprint import pprint
import json
HOST = socket.gethostname()
Addr = socket.gethostbyname(HOST)
PORT = 9527
data = {"Addr": Addr,
"PORT":PORT }
class SingleTCPHandler(socketserver.BaseRequestHandler):
"One instance per connection. Override handle(self) to customize action."
def handle(self):
# self.request is the client connection
data = self.request.recv(1024) # clip input at 1Kb
text = data.decode('utf-8')
pprint(json.loads(text))
for key in json.loads(text):
pprint(json.loads(text)[key])
self.request.send(bytes(json.dumps({"status":"success!"}), 'UTF-8'))
self.request.close()
class SimpleServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
# Ctrl-C will cleanly kill all spawned threads
daemon_threads = True
# much faster rebinding
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass):
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
if __name__ == "__main__":
server = SimpleServer((HOST, PORT), SingleTCPHandler)
print(f'Listening on {Addr} port {PORT}')
with open('server_settings.json', 'w') as f:
json.dump(data, f)
print('Dumped server addr:port to server_settings.json')
# terminate with Ctrl-C
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)