Newer
Older
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
import socket
from .. import TWSProxy
class TWSServer:
import socket
LISTENPORT = 4000
MAXCONN = 5
def run(self):
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
for port in range(self.LISTENPORT,self.LISTENPORT+10):
try:
serversocket.bind(('127.0.0.1', port))
#become a server socket
serversocket.listen(self.MAXCONN)
print("bind success on ",port)
break
except Exception as e:
print("bind failure")
print(e)
pass
print("listening on port",port)
openconnections = []
while 1:
(clientsocket, address) = serversocket.accept()
clientsocket.setblocking(True)
print("accepting connection")
tunnel = TWSProxy(clientsocket)
tunnel.daemon = True
tunnel.start()
openconnections.append(tunnel)
for c in openconnections:
if not c.is_alive():
c.join()
openconnections.remove(c)
print("there are ",len(openconnections),"current connections")