本文整理匯總了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)