本文整理汇总了Python中socket.listen方法的典型用法代码示例。如果您正苦于以下问题:Python socket.listen方法的具体用法?Python socket.listen怎么用?Python socket.listen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.listen方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: bind
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [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)
示例2: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def __init__(self, server_address, RequestHandlerClass,
ssl_context=None, request_queue_size=None):
# This overrides the implementation of __init__ in python's
# SocketServer.TCPServer (which BaseHTTPServer.HTTPServer
# does not override, thankfully).
HTTPServer.__init__(self, server_address, RequestHandlerClass)
self.socket = socket.socket(self.address_family,
self.socket_type)
self.ssl_context = ssl_context
if ssl_context:
class TSafeConnection(tsafe.Connection):
def settimeout(self, *args):
self._lock.acquire()
try:
return self._ssl_conn.settimeout(*args)
finally:
self._lock.release()
self.socket = TSafeConnection(ssl_context, self.socket)
self.server_bind()
if request_queue_size:
self.socket.listen(request_queue_size)
self.server_activate()
示例3: _set_bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def _set_bind_addr(self, value):
if isinstance(value, tuple) and value[0] in ('', None):
# Despite the socket module docs, using '' does not
# allow AI_PASSIVE to work. Passing None instead
# returns '0.0.0.0' like we want. In other words:
# host AI_PASSIVE result
# '' Y 192.168.x.y
# '' N 192.168.x.y
# None Y 0.0.0.0
# None N 127.0.0.1
# But since you can get the same effect with an explicit
# '0.0.0.0', we deny both the empty string and None as values.
raise ValueError("Host values of '' or None are not allowed. "
"Use '0.0.0.0' (IPv4) or '::' (IPv6) instead "
"to listen on all active interfaces.")
self._bind_addr = value
示例4: _resolveIPv6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def _resolveIPv6(ip, port):
"""
Resolve an IPv6 literal into an IPv6 address.
This is necessary to resolve any embedded scope identifiers to the relevant
C{sin6_scope_id} for use with C{socket.connect()}, C{socket.listen()}, or
C{socket.bind()}; see U{RFC 3493 <https://tools.ietf.org/html/rfc3493>} for
more information.
@param ip: An IPv6 address literal.
@type ip: C{str}
@param port: A port number.
@type port: C{int}
@return: a 4-tuple of C{(host, port, flow, scope)}, suitable for use as an
IPv6 address.
@raise socket.gaierror: if either the IP or port is not numeric as it
should be.
"""
return socket.getaddrinfo(ip, port, 0, 0, 0, _NUMERIC_ONLY)[0][4]
示例5: bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def bind_addr(self, value):
"""Set the interface on which to listen for connections."""
if isinstance(value, tuple) and value[0] in ('', None):
# Despite the socket module docs, using '' does not
# allow AI_PASSIVE to work. Passing None instead
# returns '0.0.0.0' like we want. In other words:
# host AI_PASSIVE result
# '' Y 192.168.x.y
# '' N 192.168.x.y
# None Y 0.0.0.0
# None N 127.0.0.1
# But since you can get the same effect with an explicit
# '0.0.0.0', we deny both the empty string and None as values.
raise ValueError(
"Host values of '' or None are not allowed. "
"Use '0.0.0.0' (IPv4) or '::' (IPv6) instead "
'to listen on all active interfaces.',
)
self._bind_addr = value
示例6: _set_bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def _set_bind_addr(self, value):
if isinstance(value, tuple) and value[0] in ('', None):
# Despite the socket module docs, using '' does not
# allow AI_PASSIVE to work. Passing None instead
# returns '0.0.0.0' like we want. In other words:
# host AI_PASSIVE result
# '' Y 192.168.x.y
# '' N 192.168.x.y
# None Y 0.0.0.0
# None N 127.0.0.1
# But since you can get the same effect with an explicit
# '0.0.0.0', we deny both the empty string and None as values.
raise ValueError("Host values of '' or None are not allowed. "
"Use '0.0.0.0' (IPv4) or '::' (IPv6) instead "
'to listen on all active interfaces.')
self._bind_addr = value
示例7: bind_addr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def bind_addr(self):
"""Return the interface on which to listen for connections.
For TCP sockets, a (host, port) tuple. Host values may be any IPv4
or IPv6 address, or any valid hostname. The string 'localhost' is a
synonym for '127.0.0.1' (or '::1', if your hosts file prefers IPv6).
The string '0.0.0.0' is a special IPv4 entry meaning "any active
interface" (INADDR_ANY), and '::' is the similar IN6ADDR_ANY for
IPv6. The empty string or None are not allowed.
For UNIX sockets, supply the filename as a string.
Systemd socket activation is automatic and doesn't require tempering
with this variable.
"""
return self._bind_addr
示例8: socket_host
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def socket_host(self): # noqa: D401; irrelevant for properties
"""The hostname or IP address on which to listen for connections.
Host values may be any IPv4 or IPv6 address, or any valid hostname.
The string 'localhost' is a synonym for '127.0.0.1' (or '::1', if
your hosts file prefers IPv6). The string '0.0.0.0' is a special
IPv4 entry meaning "any active interface" (INADDR_ANY), and '::'
is the similar IN6ADDR_ANY for IPv6. The empty string or None are
not allowed.
"""
return self._socket_host
示例9: bind_unix_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):
"""Creates a listening unix socket.
If a socket with the given name already exists, it will be deleted.
If any other file with that name exists, an exception will be
raised.
Returns a socket object (not a list of socket objects like
`bind_sockets`)
"""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
set_close_exec(sock.fileno())
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
try:
st = os.stat(file)
except OSError as err:
if errno_from_exception(err) != errno.ENOENT:
raise
else:
if stat.S_ISSOCK(st.st_mode):
os.remove(file)
else:
raise ValueError("File %s exists and is not a socket", file)
sock.bind(file)
os.chmod(file, mode)
sock.listen(backlog)
return sock
示例10: listen
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def listen(self, port, address=""):
u"""开始在给定的端口接收连接.
这个方法可能不只被调用一次, 可能会在多个端口上被调用多次.
`listen` 方法将立即生效, 所以它没必要在 `TCPServer.start` 之后调用.
然而, 必须要启动 `.IOLoop` 才可以.
"""
sockets = bind_sockets(port, address=address)
self.add_sockets(sockets)
示例11: startVideoGeneratorServer
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def startVideoGeneratorServer():
server_address = (settings.server_location, int(settings.server_port_vid_gen))
print('Starting video generator server on %s port %s' % server_address)
socket.bind(server_address)
socket.listen(5)
thread = Thread(target=waitConnect)
thread.start()
servertick = Thread(target=serverTick)
servertick.start()
开发者ID:HA6Bots,项目名称:Automatic-Youtube-Reddit-Text-To-Speech-Video-Generator-and-Uploader,代码行数:11,代码来源:socketservervideogenerator.py
示例12: bind_unix_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def bind_unix_socket(
file: str, mode: int = 0o600, backlog: int = _DEFAULT_BACKLOG
) -> socket.socket:
"""Creates a listening unix socket.
If a socket with the given name already exists, it will be deleted.
If any other file with that name exists, an exception will be
raised.
Returns a socket object (not a list of socket objects like
`bind_sockets`)
"""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
set_close_exec(sock.fileno())
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error as e:
if errno_from_exception(e) != errno.ENOPROTOOPT:
# Hurd doesn't support SO_REUSEADDR
raise
sock.setblocking(False)
try:
st = os.stat(file)
except OSError as err:
if errno_from_exception(err) != errno.ENOENT:
raise
else:
if stat.S_ISSOCK(st.st_mode):
os.remove(file)
else:
raise ValueError("File %s exists and is not a socket", file)
sock.bind(file)
os.chmod(file, mode)
sock.listen(backlog)
return sock
示例13: listen
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def listen(self, port: int, address: str = "") -> None:
"""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)
示例14: bind
# 需要导入模块: import socket [as 别名]
# 或者: from socket import listen [as 别名]
def bind(
self,
port: int,
address: str = None,
family: socket.AddressFamily = socket.AF_UNSPEC,
backlog: int = 128,
reuse_port: bool = False,
) -> None:
"""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)