-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp-nat-server.py
52 lines (42 loc) · 1.44 KB
/
tcp-nat-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
import socket
import sys
import thread
import threading
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
s.bind((sys.argv[1], int(sys.argv[2])))
clients = []
client_own_info = []
def session(c, addr):
print("Client thread started.")
while True:
print("Thread for %s responding" % str(addr))
#c.send("Your addr:port pair is: %s" % str(addr))
#ie. if there is another client waiting, send details to connected client
if len(clients) > 1:
for i in range(len(clients)):
if clients[i] != addr:
info = str(str(clients[i])[2:-1] + client_own_info[i])
c.send(info)
c.close()
print("Client thread terminating...")
thread.exit()
time.sleep(3)
def monitor(s):
s.listen(5)
while True:
c, addr = s.accept()
print("Connection from %s" % str(addr))
clients.append(addr)
client_private = c.recv(1024)
client_own_info.append(client_private)
print(client_own_info)
try:
client_thread = threading.Thread(target=session, args=(c,addr))
client_thread.start()
#thread.start_new_thread(session(c, addr))
except:
print("Error starting client thread for %s." % str(addr))
monitor(s)