コード例 #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
コード例 #2
0
ファイル: test_pydevconsole.py プロジェクト: MrGreen123/Pydev
    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
コード例 #3
0
class NetworkListener(Thread):
    """Use a new thread to listen to a network port and place each message
    sent on a new line to the message queue"""
    
    def __init__(self):
        self._serverSocket = ServerSocket(PORT)
        self._isListening = True
        Thread.__init__(self)
        log("Started server on " + str(PORT))
        
    def run(self):
        """Start thread listening to the socket"""
        while(self._isListening):
            self._clientSocket = self._serverSocket.accept()
            self._handleConnection(self._clientSocket)
        
    def _handleConnection(self, socket):
        reader = BufferedReader(InputStreamReader(socket.getInputStream()))

        try:
            line = reader.readLine();
    
            while(line is not None and not self._serverSocket.closed):
                self._handleMessage(line)
                line = reader.readLine();
        except SocketException:
            if self._isListening:
                raise SocketException
            else:
                log("Ignoring socket exception during shutdown")

        socket.close()

    def _handleMessage(self, message):
        """Called for each message received."""
        lock.acquire()
        messageQueue.append(message)
        lock.notifyAll()
        lock.release()

    def stop(self):
        """Stop the server socket and close any open client socket"""
        self._isListening = False

        if(self._serverSocket is not None):
            self._serverSocket.close()

        if(self._clientSocket is not None):
            self._clientSocket.close() 
コード例 #4
0
class server(object):
    reg_obj_ip = []

    def __init__(self):
        self.ss = ServerSocket(8888)

    def start(self):
        while True:
            self.s = self.ss.accept()
            self.reg_obj_ip.append(self.s)
コード例 #5
0
class server(object):
    def __init__(self):
        self.ss = ServerSocket(777)
        self.s = None
        print("accpted", self.s)

    def server_run(self):
        while True:
            self.s = self.ss.accept()
            multi_client(self.s).start()
コード例 #6
0
ファイル: Test_Http.py プロジェクト: hobson/ggpy
 def __init__(self):
     """ generated source for method __init__ """
     #  Create a server socket on the first available port.
     defaultTestingPort = 13174
     ss = None
     while True:
         try:
             ss = ServerSocket(defaultTestingPort)
         except Exception as e:
             ss = None
             defaultTestingPort += 1
         if not ((ss == None)):
             break
     try:
         self.client = Socket("127.0.0.1", defaultTestingPort)
         self.server = ss.accept()
     except Exception as e:
         fail("Could not establish connection: " + e)
         e.printStackTrace()
     assertNotNull(self.client)
     assertNotNull(self.server)
コード例 #7
0
class server(Thread):
	def __init__(self,file_name):
		self.list = []
		self.ss = ServerSocket(777)
		f =  open(file_name,"r")
		self.list = f.readlines()

	def run(self):

		while len(self.list):
			s = self.ss.accept()
			server_multi(s,self.list[0]).start()
			self.list.pop(0)
コード例 #8
0
class Server(object):
    def __init__(self, port):
        self.port = port
        self._listener = ServerSocket(self.port)
        self.speaker = Speaker()
        self.speaker.playAll()
        self.connections = []

    def listen(self):
        while True:
            newConnection = ClientConnection(self,
                                             self._listener.accept(),
                                             self.speaker.newConnection())
            print 'New connection accepted!', newConnection
            self.connections.append(newConnection)
            Thread(target=newConnection.handle).start()

    def deleteConnection(self, connectionTrack):
        self.speaker.deleteConnection(connectionTrack)
コード例 #9
0
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
コード例 #10
0
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
コード例 #11
0
 def __init__(self, port):
     self.port = port
     self._listener = ServerSocket(self.port)
     self.speaker = Speaker()
     self.speaker.playAll()
     self.connections = []
コード例 #12
0
 def __init__(self):
     self._serverSocket = ServerSocket(PORT)
     self._isListening = True
     Thread.__init__(self)
     log("Started server on " + str(PORT))
コード例 #13
0
 def find_free_port(self, socket=ServerSocket(0)):
     try:
         return socket.getLocalPort()
     finally:
         socket.close()
コード例 #14
0
 def __init__(self):
     self.ss = ServerSocket(8888)
コード例 #15
0
 def __init__(self):
     self.ss = ServerSocket(777)
     self.s = None
     print("accpted", self.s)
コード例 #16
0
	def __init__(self,file_name):
		self.list = []
		self.ss = ServerSocket(777)
		f =  open(file_name,"r")
		self.list = f.readlines()
コード例 #17
0
def portStartListening(tcpPort):
    sskt = ServerSocket(tcpPort.port, tcpPort.backlog)
    tcpPort.sskt = sskt
    AcceptBlocker(tcpPort, theMultiplexor.q).start()
コード例 #18
0
from java.net import ServerSocket as SS
from java.net import Socket as SO
from java.io import InputStream
from java.io import OutputStream
from java.io import PrintStream as PS
from java.util import Scanner as S

SS_1 = SS(777)
SO_1 = SS_1.accept()
IS_1 = S(SO_1.getInputStream())
OS_1 = PS(SO_1.getOutputStream())
while True:
    if IS_1.hasNext():
        print(IS_1.next())
        print(OS_1.println("received..."))
    else:
        break