Skip to content
Snippets Groups Projects
__init__.py 1.28 KiB
Newer Older
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")