Exemple #1
0
    def getFreeAddresses(self):
        import socket
        s = socket.socket()
        s.bind(('', 0))
        port0 = s.getsockname()[1]

        s1 = socket.socket()
        s1.bind(('', 0))
        port1 = s1.getsockname()[1]
        s.close()
        s1.close()

        if port0 <= 0 or port1 <= 0:
            #This happens in Jython...
            from java.net import ServerSocket
            s0 = ServerSocket(0)
            port0 = s0.getLocalPort()

            s1 = ServerSocket(0)
            port1 = s1.getLocalPort()

            s0.close()
            s1.close()

        assert port0 != port1
        assert port0 > 0
        assert port1 > 0

        return port0, port1
def get_socket_names(n_sockets, close=False):
    socket_names = []
    sockets = []
    for _ in range(n_sockets):
        if IS_JYTHON:
            # Although the option which would be pure java *should* work for Jython, the socket being returned is still 0
            # (i.e.: it doesn't give the local port bound, only the original port, which was 0).
            from java.net import ServerSocket
            sock = ServerSocket(0)
            socket_name = get_localhost(), sock.getLocalPort()
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind((get_localhost(), 0))
            socket_name = sock.getsockname()

        sockets.append(sock)
        socket_names.append(socket_name)

    if close:
        for s in sockets:
            s.close()
    return socket_names
 def __init__(self):
     self._serverSocket = ServerSocket(PORT)
     self._isListening = True
     Thread.__init__(self)
     log("Started server on " + str(PORT))
Exemple #4
0
def portStartListening(tcpPort):
    sskt = ServerSocket(tcpPort.port, tcpPort.backlog)
    tcpPort.sskt = sskt
    AcceptBlocker(tcpPort, theMultiplexor.q).start()
Exemple #5
0
 def __init__(self):
     self.ss = ServerSocket(8888)
Exemple #6
0
 def __init__(self):
     self.ss = ServerSocket(777)
     self.s = None
     print("accpted", self.s)
Exemple #7
0
	def __init__(self,file_name):
		self.list = []
		self.ss = ServerSocket(777)
		f =  open(file_name,"r")
		self.list = f.readlines()
Exemple #8
0
 def find_free_port(self, socket=ServerSocket(0)):
     try:
         return socket.getLocalPort()
     finally:
         socket.close()