-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession-manager.py
executable file
·48 lines (44 loc) · 1.34 KB
/
session-manager.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
#!/usr/bin/env python3
import socket
from socket import errno
import time
from random import randint
import subprocess
def getPort():
port = randint(42000, 42100)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', port))
except socket.error as e:
if e.errno == errno.EADDRINUSE:
s.close()
return getPort()
else:
s.close()
return port
if __name__ == '__main__':
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 4200))
s.listen(1)
waitlist = []
while True:
conn, addr = s.accept()
waitlist.append(conn)
if len(waitlist) == 2:
port = getPort()
print("Starting server on port %d" % port)
subprocess.Popen(["./server.py", str(port)])
for conn in waitlist:
conn.sendall(("10.42.0.146:" + str(port) + "\n").encode('utf-8'))
while len(waitlist) > 0:
conn = waitlist.pop()
try:
conn.shutdown(socket.SHUT_RDWR)
except:
pass
try:
conn.close()
except:
pass