コード例 #1
0
 def __init__(self, family):
     super(TestTCPServer, self).__init__()
     self.streams = []
     self.queue = Queue()
     sockets = bind_sockets(None, 'localhost', family)
     self.add_sockets(sockets)
     self.port = sockets[0].getsockname()[1]
コード例 #2
0
    def listen(self, port, address=""):
        """Starts accepting connections on the given port.

        This method may be called more than once to listen on multiple ports.
        `listen` takes effect immediately; it is not necessary to call
        `TCPServer.start` afterwards.  It is, however, necessary to start
        the `.IOLoop`.
        """
        sockets = bind_sockets(port, address=address)
        self.add_sockets(sockets)
コード例 #3
0
 def test_reuse_port(self):
     sockets = []
     socket, port = bind_unused_port(reuse_port=True)
     try:
         sockets = bind_sockets(port, '127.0.0.1', reuse_port=True)
         self.assertTrue(all(s.getsockname()[1] == port for s in sockets))
     finally:
         socket.close()
         for sock in sockets:
             sock.close()
コード例 #4
0
 def test_same_port_allocation(self):
     if 'TRAVIS' in os.environ:
         self.skipTest(
             "dual-stack servers often have port conflicts on travis")
     sockets = bind_sockets(None, 'localhost')
     try:
         port = sockets[0].getsockname()[1]
         self.assertTrue(
             all(s.getsockname()[1] == port for s in sockets[1:]))
     finally:
         for sock in sockets:
             sock.close()
コード例 #5
0
ファイル: testing.py プロジェクト: crimv42/saltstack
def bind_unused_port(reuse_port=False):
    """Binds a server socket to an available port on localhost.

    Returns a tuple (socket, port).

    .. versionchanged:: 4.4
       Always binds to ``127.0.0.1`` without resolving the name
       ``localhost``.
    """
    sock = netutil.bind_sockets(None, '127.0.0.1', family=socket.AF_INET,
                                reuse_port=reuse_port)[0]
    port = sock.getsockname()[1]
    return sock, port
コード例 #6
0
    def test_ipv6(self):
        [sock] = bind_sockets(None, '::1', family=socket.AF_INET6)
        port = sock.getsockname()[1]
        self.http_server.add_socket(sock)
        url = '%s://[::1]:%d/hello' % (self.get_protocol(), port)

        # ipv6 is currently enabled by default but can be disabled
        self.http_client.fetch(url, self.stop, allow_ipv6=False)
        response = self.wait()
        self.assertEqual(response.code, 599)

        self.http_client.fetch(url, self.stop)
        response = self.wait()
        self.assertEqual(response.body, b"Hello world!")
コード例 #7
0
    def bind(self,
             port,
             address=None,
             family=socket.AF_UNSPEC,
             backlog=128,
             reuse_port=False):
        """Binds this server to the given port on the given address.

        To start the server, call `start`. If you want to run this server
        in a single process, you can call `listen` as a shortcut to the
        sequence of `bind` and `start` calls.

        Address may be either an IP address or hostname.  If it's a hostname,
        the server will listen on all IP addresses associated with the
        name.  Address may be an empty string or None to listen on all
        available interfaces.  Family may be set to either `socket.AF_INET`
        or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise
        both will be used if available.

        The ``backlog`` argument has the same meaning as for
        `socket.listen <socket.socket.listen>`. The ``reuse_port`` argument
        has the same meaning as for `.bind_sockets`.

        This method may be called multiple times prior to `start` to listen
        on multiple ports or interfaces.

        .. versionchanged:: 4.4
           Added the ``reuse_port`` argument.
        """
        sockets = bind_sockets(port,
                               address=address,
                               family=family,
                               backlog=backlog,
                               reuse_port=reuse_port)
        if self._started:
            self.add_sockets(sockets)
        else:
            self._pending_sockets.extend(sockets)
コード例 #8
0
 def _sock_default(self):
     return netutil.bind_sockets(self.port,
                                 "127.0.0.1",
                                 family=socket.AF_INET,
                                 reuse_port=False)[0]