本文整理汇总了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!")
示例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)
示例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")
示例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
示例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)
示例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")
示例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
示例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
示例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
示例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()
示例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()
示例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]
示例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)
示例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)
示例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