-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·125 lines (99 loc) · 4.18 KB
/
server.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
#!/usr/bin/env python3
"""
Created by Mike Czarny on 11/17/2018 as a code challenge for blockchain.com
github.com/r3lik
"""
import argparse
import socket
import threading
import uuid
import etcd3
UUID=str(uuid.uuid4()) # set random UUID for server and convert to string
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-H','--host', type=str, default='0.0.0.0')
parser.add_argument('-p','--port', type=int, default=5151) # internally in the Docker container the port is 5151
result = parser.parse_args()
return result
def threaded_client(client_list, conn, addr):
""" Threads single client connections"""
client_id = "{0}:{1}".format(addr[0],addr[1]) # address comes from server func
ETCD_HOST = 'etcd-cluster.socketz.gg' # DNS alias of three ETCD instances. In the "real world", this would be a FQDN that resolves to multiple IPs (roundrobin DNS).
ETCD_PORT = 2379
ETCD_CLIENTS_LIST = '/sockets/client_list_cnt'
etcd = etcd3.client(host=ETCD_HOST,port=ETCD_PORT)
def clients_increment(key):
curval = etcd.get(key)[0] #returns tuple
if curval:
newval = int(curval.decode('ASCII')) + 1
else:
newval = 1
etcd.put(key,str(newval).encode('ASCII'))
return
def clients_decrement(key):
curval = etcd.get(key)[0]
if curval:
newval = int(curval.decode('ASCII')) - 1
else:
newval = 0
etcd.put(key,str(newval).encode('ASCII'))
return
def clients_get(key):
curval = etcd.get(key)[0].decode('ASCII')
return int(curval)
try:
client_list.add(client_id)
clients_increment(ETCD_CLIENTS_LIST)
while True:
data = conn.recv(2048)
if not data:
break
elif data.decode('ASCII').strip() == "WHY":
reply = '42\n'
conn.sendall(reply.encode('ASCII'))
print("sent reply to {0}".format(client_id))
elif data.decode('ASCII').strip() == "WHO":
reply1 = "IP:PORT of clients connected to this server: {0}\n".format(client_list) # IP:PORT is of the HAProxy frontend
reply2 = "clients connected to this server: {0}\n".format(str(len(client_list)))
reply3 = "clients connected to all servers: {0}\n".format(clients_get(ETCD_CLIENTS_LIST))
conn.sendall(reply1.encode('ASCII'))
conn.sendall(reply2.encode('ASCII'))
conn.sendall(reply3.encode('ASCII'))
print("sent reply to {0}".format(client_id))
elif data.decode('ASCII').strip() == "WHERE":
reply = "{0}\n".format(UUID)
conn.sendall(reply.encode('ASCII'))
print("sent reply to {0}".format(client_id))
elif data.decode('ASCII').strip() == "QUIT":
break
else:
reply = "invalid command:\n use 'WHY', 'WHO', 'WHERE', 'QUIT'\n"
conn.sendall(reply.encode('ASCII'))
conn.shutdown(socket.SHUT_RDWR)
except socket.error as err:
print("socket disconnected...")
finally:
client_list.remove(client_id)
clients_decrement(ETCD_CLIENTS_LIST)
conn.close()
def server(host, port, client_list):
print("starting server...")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #setting sockopt so addresses are released after script is killed. Otherwise have to kill PID.
s.bind((host, port))
s.listen(5)
while True:
conn, addr = s.accept()
# print(addr) # debugging
print('connection from: '+addr[0]+':'+str(addr[1]))
t = threading.Thread(target=threaded_client, daemon = True, args=(client_list, conn, addr)) # daemon thread terminates when main program ends
t.start()
def main():
client_list = set() # stores an array of currently connected clients. FOR DEBUGGING. removes client after d/c.
args = parse_args()
try:
server(args.host, args.port, client_list)
except KeyboardInterrupt:
print("keyboard interrupt")
if __name__ == '__main__':
main()