當前位置: 首頁>>代碼示例>>Python>>正文


Python netutil.bind_sockets方法代碼示例

本文整理匯總了Python中tornado.netutil.bind_sockets方法的典型用法代碼示例。如果您正苦於以下問題:Python netutil.bind_sockets方法的具體用法?Python netutil.bind_sockets怎麽用?Python netutil.bind_sockets使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tornado.netutil的用法示例。


在下文中一共展示了netutil.bind_sockets方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_ipv6

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def test_ipv6(self):
        try:
            [sock] = bind_sockets(None, '::1', family=socket.AF_INET6)
            port = sock.getsockname()[1]
            self.http_server.add_socket(sock)
        except socket.gaierror as e:
            if e.args[0] == socket.EAI_ADDRFAMILY:
                # python supports ipv6, but it's not configured on the network
                # interface, so skip this test.
                return
            raise
        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!") 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:23,代碼來源:simple_httpclient_test.py

示例2: bind

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def bind(self, port, address=None, family=socket.AF_UNSPEC, backlog=128):
        u"""綁定該服務到指定的地址的指定端口上.

        要啟動該服務, 調用 `start`. 如果你想要在一個單進程上運行該服務,
        你可以調用 `listen` 作為順序調用 `bind` 和 `start` 的一個快捷方式.

        address 參數可以是 IP 地址或者主機名.  如果它是主機名,
        該服務將監聽在和該名稱有關的所有 IP 地址上.  地址也可以是空字符串或者
        None, 服務將監聽所有可用的接口. family 可以被設置為 `socket.AF_INET` 或
        `socket.AF_INET6` 用來限定是 IPv4 或 IPv6 地址, 否則如果可用的話, 兩者
        都將被使用.

        ``backlog`` 參數和 `socket.listen <socket.socket.listen>` 是相同含義.

        這個方法可能在 `start` 之前被調用多次來監聽在多個端口或接口上.
        """
        sockets = bind_sockets(port, address=address, family=family,
                               backlog=backlog)
        if self._started:
            self.add_sockets(sockets)
        else:
            self._pending_sockets.extend(sockets) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:24,代碼來源:tcpserver.py

示例3: test_advanced

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def test_advanced(self):
        code = textwrap.dedent(
            """
            from tornado.ioloop import IOLoop
            from tornado.netutil import bind_sockets
            from tornado.process import fork_processes, task_id
            from tornado.ioloop import IOLoop
            from tornado.tcpserver import TCPServer

            sockets = bind_sockets(0, address='127.0.0.1')
            fork_processes(3)
            server = TCPServer()
            server.add_sockets(sockets)
            IOLoop.current().run_sync(lambda: None)
            print(task_id(), end='')
        """
        )
        out = self.run_subproc(code)
        self.assertEqual("".join(sorted(out)), "012") 
開發者ID:opendevops-cn,項目名稱:opendevops,代碼行數:21,代碼來源:tcpserver_test.py

示例4: load_httpserver

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def load_httpserver(self, sockets=None, **kwargs):
        if not sockets:
            from tornado.netutil import bind_sockets

            if settings.IPV4_ONLY:
                import socket

                sockets = bind_sockets(options.port, options.address, family=socket.AF_INET)
            else:
                sockets = bind_sockets(options.port, options.address)

        http_server = tornado.httpserver.HTTPServer(self.application, **kwargs)

        http_server.add_sockets(sockets)
        self.httpserver = http_server
        return self.httpserver 
開發者ID:mqingyn,項目名稱:torngas,代碼行數:18,代碼來源:webserver.py

示例5: add_sockets

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def add_sockets(self, sockets):
        """Makes this server start accepting connections on the given sockets.

        The ``sockets`` parameter is a list of socket objects such as
        those returned by `~tornado.netutil.bind_sockets`.
        `add_sockets` is typically used in combination with that
        method and `tornado.process.fork_processes` to provide greater
        control over the initialization of a multi-process server.
        """
        if self.io_loop is None:
            self.io_loop = IOLoop.current()

        for sock in sockets:
            self._sockets[sock.fileno()] = sock
            add_accept_handler(sock, self._handle_connection,
                               io_loop=self.io_loop) 
開發者ID:viewfinderco,項目名稱:viewfinder,代碼行數:18,代碼來源:tcpserver.py

示例6: test_advanced

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def test_advanced(self):
        code = textwrap.dedent("""
            from __future__ import print_function
            from tornado.ioloop import IOLoop
            from tornado.netutil import bind_sockets
            from tornado.process import fork_processes, task_id
            from tornado.ioloop import IOLoop
            from tornado.tcpserver import TCPServer

            sockets = bind_sockets(0, address='127.0.0.1')
            fork_processes(3)
            server = TCPServer()
            server.add_sockets(sockets)
            IOLoop.current().run_sync(lambda: None)
            print(task_id(), end='')
        """)
        out = self.run_subproc(code)
        self.assertEqual(''.join(sorted(out)), "012") 
開發者ID:tp4a,項目名稱:teleport,代碼行數:20,代碼來源:tcpserver_test.py

示例7: make_iostream_pair

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def make_iostream_pair(self, **kwargs):
        port = get_unused_port()
        [listener] = netutil.bind_sockets(port, '127.0.0.1',
                                          family=socket.AF_INET)
        streams = [None, None]

        def accept_callback(connection, address):
            streams[0] = IOStream(connection, io_loop=self.io_loop, **kwargs)
            self.stop()

        def connect_callback():
            streams[1] = client_stream
            self.stop()
        netutil.add_accept_handler(listener, accept_callback,
                                   io_loop=self.io_loop)
        client_stream = IOStream(socket.socket(), io_loop=self.io_loop,
                                 **kwargs)
        client_stream.connect(('127.0.0.1', port),
                              callback=connect_callback)
        self.wait(condition=lambda: all(streams))
        self.io_loop.remove_handler(listener.fileno())
        listener.close()
        return streams 
開發者ID:omererdem,項目名稱:honeything,代碼行數:25,代碼來源:iostream_test.py

示例8: test_multi_process

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def test_multi_process(self):
        self.assertFalse(IOLoop.initialized())
        port = get_unused_port()

        def get_url(path):
            return "http://127.0.0.1:%d%s" % (port, path)
        sockets = bind_sockets(port, "127.0.0.1")
        # ensure that none of these processes live too long
        signal.alarm(5)
        try:
            id = fork_processes(3, max_restarts=3)
        except SystemExit, e:
            # if we exit cleanly from fork_processes, all the child processes
            # finished with status 0
            self.assertEqual(e.code, 0)
            self.assertTrue(task_id() is None)
            for sock in sockets:
                sock.close()
            return 
開發者ID:omererdem,項目名稱:honeything,代碼行數:21,代碼來源:process_test.py

示例9: bind_unused_port

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def bind_unused_port(reuse_port=False):
    """Binds a server socket to an available port on localhost.

    Returns a tuple (socket, port).
    """
    [sock] = netutil.bind_sockets(None, 'localhost', family=socket.AF_INET,
                                  reuse_port=reuse_port)
    port = sock.getsockname()[1]
    return sock, port 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:11,代碼來源:testing.py

示例10: test_same_port_allocation

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
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() 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:13,代碼來源:netutil_test.py

示例11: test_reuse_port

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def test_reuse_port(self):
        socket, port = bind_unused_port(reuse_port=True)
        try:
            sockets = bind_sockets(port, 'localhost', reuse_port=True)
            self.assertTrue(all(s.getsockname()[1] == port for s in sockets))
        finally:
            socket.close()
            for sock in sockets:
                sock.close() 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:11,代碼來源:netutil_test.py

示例12: __init__

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def __init__(self, family):
        super(TestTCPServer, self).__init__()
        self.streams = []
        sockets = bind_sockets(None, 'localhost', family)
        self.add_sockets(sockets)
        self.port = sockets[0].getsockname()[1] 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:8,代碼來源:tcpclient_test.py

示例13: add_sockets

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def add_sockets(self, sockets):
        u"""使服務開始接收給定端口的連接.

        ``sockets`` 參數是一個 socket 對象的列表, 例如那些被
        `~tornado.netutil.bind_sockets` 所返回的對象.
        `add_sockets` 通常和 `tornado.process.fork_processes` 相結合使用,
        以便於在一個多進程服務初始化時提供更多控製.
        """
        if self.io_loop is None:
            self.io_loop = IOLoop.current()

        for sock in sockets:
            self._sockets[sock.fileno()] = sock
            add_accept_handler(sock, self._handle_connection,
                               io_loop=self.io_loop) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:17,代碼來源:tcpserver.py

示例14: listen

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def listen(self, port, address=""):
        u"""開始在給定的端口接收連接.

        這個方法可能不隻被調用一次, 可能會在多個端口上被調用多次.
        `listen` 方法將立即生效, 所以它沒必要在 `TCPServer.start` 之後調用.
        然而, 必須要啟動 `.IOLoop` 才可以.
        """
        sockets = bind_sockets(port, address=address)
        self.add_sockets(sockets) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:11,代碼來源:tcpserver.py

示例15: bind_unused_port

# 需要導入模塊: from tornado import netutil [as 別名]
# 或者: from tornado.netutil import bind_sockets [as 別名]
def bind_unused_port(reuse_port: bool = False) -> Tuple[socket.socket, int]:
    """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(
        0, "127.0.0.1", family=socket.AF_INET, reuse_port=reuse_port
    )[0]
    port = sock.getsockname()[1]
    return sock, port 
開發者ID:opendevops-cn,項目名稱:opendevops,代碼行數:16,代碼來源:testing.py


注:本文中的tornado.netutil.bind_sockets方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。