本文整理匯總了Python中socket.AI_PASSIVE屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.AI_PASSIVE屬性的具體用法?Python socket.AI_PASSIVE怎麽用?Python socket.AI_PASSIVE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.AI_PASSIVE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: select_address_family
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def select_address_family(host, port):
"""Return ``AF_INET4``, ``AF_INET6``, or ``AF_UNIX`` depending on
the host and port."""
# disabled due to problems with current ipv6 implementations
# and various operating systems. Probably this code also is
# not supposed to work, but I can't come up with any other
# ways to implement this.
# try:
# info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
# socket.SOCK_STREAM, 0,
# socket.AI_PASSIVE)
# if info:
# return info[0][0]
# except socket.gaierror:
# pass
if host.startswith("unix://"):
return socket.AF_UNIX
elif ":" in host and hasattr(socket, "AF_INET6"):
return socket.AF_INET6
return socket.AF_INET
示例2: test_basic_behavior
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def test_basic_behavior(self):
inet4_server = self.mox.CreateMock(wsgi_server._SingleAddressWsgiServer)
inet6_server = self.mox.CreateMock(wsgi_server._SingleAddressWsgiServer)
self.mox.StubOutWithMock(wsgi_server, '_SingleAddressWsgiServer')
self.mox.StubOutWithMock(socket, 'getaddrinfo')
socket.getaddrinfo('localhost', 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
socket.AI_PASSIVE).AndReturn(
[(None, None, None, None, ('127.0.0.1', 0, 'baz')),
(None, None, None, None, ('::1', 0, 'baz'))])
wsgi_server._SingleAddressWsgiServer(('127.0.0.1', 0), None).AndReturn(
inet4_server)
inet4_server.start()
inet4_server.port = 123
wsgi_server._SingleAddressWsgiServer(('::1', 123), None).AndReturn(
inet6_server)
inet6_server.start()
self.mox.ReplayAll()
self.server.start()
self.mox.VerifyAll()
self.assertItemsEqual([inet4_server, inet6_server],
self.server._servers)
示例3: test_retry_limited
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def test_retry_limited(self):
inet4_servers = [self.mox.CreateMock(wsgi_server._SingleAddressWsgiServer)
for _ in range(wsgi_server._PORT_0_RETRIES)]
inet6_servers = [self.mox.CreateMock(wsgi_server._SingleAddressWsgiServer)
for _ in range(wsgi_server._PORT_0_RETRIES)]
self.mox.StubOutWithMock(wsgi_server, '_SingleAddressWsgiServer')
self.mox.StubOutWithMock(socket, 'getaddrinfo')
socket.getaddrinfo('localhost', 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
socket.AI_PASSIVE).AndReturn(
[(None, None, None, None, ('127.0.0.1', 0, 'baz')),
(None, None, None, None, ('::1', 0, 'baz'))])
for offset, (inet4_server, inet6_server) in enumerate(zip(
inet4_servers, inet6_servers)):
wsgi_server._SingleAddressWsgiServer(('127.0.0.1', 0), None).AndReturn(
inet4_server)
inet4_server.start()
inet4_server.port = offset + 1
wsgi_server._SingleAddressWsgiServer(('::1', offset + 1), None).AndReturn(
inet6_server)
inet6_server.start().AndRaise(
wsgi_server.BindError('message', (errno.EADDRINUSE, 'in use')))
inet4_server.quit()
self.mox.ReplayAll()
self.assertRaises(wsgi_server.BindError, self.server.start)
self.mox.VerifyAll()
示例4: test_ignore_other_errors
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def test_ignore_other_errors(self):
inet4_server = self.mox.CreateMock(wsgi_server._SingleAddressWsgiServer)
inet6_server = self.mox.CreateMock(wsgi_server._SingleAddressWsgiServer)
self.mox.StubOutWithMock(wsgi_server, '_SingleAddressWsgiServer')
self.mox.StubOutWithMock(socket, 'getaddrinfo')
socket.getaddrinfo('localhost', 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
socket.AI_PASSIVE).AndReturn(
[(None, None, None, None, ('127.0.0.1', 0, 'baz')),
(None, None, None, None, ('::1', 0, 'baz'))])
wsgi_server._SingleAddressWsgiServer(('127.0.0.1', 0), None).AndReturn(
inet4_server)
inet4_server.start()
inet4_server.port = 123
wsgi_server._SingleAddressWsgiServer(('::1', 123), None).AndReturn(
inet6_server)
inet6_server.start().AndRaise(
wsgi_server.BindError('message', (errno.ENOPROTOOPT, 'no protocol')))
self.mox.ReplayAll()
self.server.start()
self.mox.VerifyAll()
self.assertItemsEqual([inet4_server],
self.server._servers)
示例5: select_ip_version
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def select_ip_version(host, port):
"""Returns AF_INET4 or AF_INET6 depending on where to connect to."""
# disabled due to problems with current ipv6 implementations
# and various operating systems. Probably this code also is
# not supposed to work, but I can't come up with any other
# ways to implement this.
# try:
# info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
# socket.SOCK_STREAM, 0,
# socket.AI_PASSIVE)
# if info:
# return info[0][0]
# except socket.gaierror:
# pass
if ':' in host and hasattr(socket, 'AF_INET6'):
return socket.AF_INET6
return socket.AF_INET
示例6: _set_bind_addr
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [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: get_listen_ip
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def get_listen_ip():
""" Get IP to listen on for things that don't natively support detecting IPv4/IPv6 dualstack """
def has_dual_stack():
if hasattr(socket, 'AF_INET6') and hasattr(socket, 'IPPROTO_IPV6') and hasattr(socket, 'IPV6_V6ONLY'):
sock = None
try:
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
import urllib3
return urllib3.util.connection.HAS_IPV6
except socket.error as e:
logging.debug('Error when working with ipv6 socket: %s', e)
finally:
if sock:
sock.close()
return False
info = socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
# in case dual stack is not supported we want IPv4 to be preferred over IPv6
info.sort(key=lambda x: x[0] == socket.AF_INET, reverse=not has_dual_stack())
return info[0][4][0]
示例8: bind_tcp_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def bind_tcp_socket(address):
"""Takes (host, port) and returns (socket_object, (host, port)).
If the passed-in port is None, bind an unused port and return it.
"""
host, port = address
for res in set(socket.getaddrinfo(host, port, socket.AF_INET,
socket.SOCK_STREAM, 0,
socket.AI_PASSIVE)):
family, socktype, proto, _, sock_addr = res
sock = socket.socket(family, socktype, proto)
if os.name != 'nt':
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Automatic port allocation with port=None.
sock.bind(sock_addr)
sock.listen(128)
bound_port = sock.getsockname()[1]
return sock, (host, bound_port)
raise socket.error('could not bind socket')
示例9: create_custom_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def create_custom_socket(host, port, *, # pylint: disable=too-many-locals
family=socket.AF_UNSPEC,
type=socket.SOCK_STREAM, # pylint: disable=redefined-builtin
flags=socket.AI_PASSIVE,
options=None,
loop=None):
if loop is None:
loop = asyncio.get_event_loop()
res = await loop.getaddrinfo(host, port,
family=family, type=type, flags=flags)
af, s_typ, proto, _, sa = res[0] # pylint: disable=invalid-name
sock = socket.socket(af, s_typ, proto)
if options is not None:
for level, optname, val in options:
sock.setsockopt(level, optname, val)
sock.bind(sa)
return sock
示例10: start
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def start(self):
"""Starts the WsgiServer.
This starts multiple _SingleAddressWsgiServers to bind the address in all
address families.
Raises:
BindError: The address could not be bound.
"""
host, port = self.bind_addr
try:
info = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0, socket.AI_PASSIVE)
except socket.gaierror:
if ':' in host:
info = [(socket.AF_INET6, socket.SOCK_STREAM, 0, '', self.bind_addr)]
else:
info = [(socket.AF_INET, socket.SOCK_STREAM, 0, '', self.bind_addr)]
if port != 0:
self._start_all_fixed_port(info)
else:
for _ in range(_PORT_0_RETRIES):
if self._start_all_dynamic_port(info):
break
else:
raise BindError('Unable to find a consistent port %s' % host)
示例11: test_start_some_fail_to_bind
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def test_start_some_fail_to_bind(self):
failing_server = self.mox.CreateMock(
wsgi_server._SingleAddressWsgiServer)
starting_server = self.mox.CreateMock(
wsgi_server._SingleAddressWsgiServer)
another_starting_server = self.mox.CreateMock(
wsgi_server._SingleAddressWsgiServer)
self.mox.StubOutWithMock(wsgi_server, '_SingleAddressWsgiServer')
self.mox.StubOutWithMock(socket, 'getaddrinfo')
socket.getaddrinfo('localhost', 123, socket.AF_UNSPEC, socket.SOCK_STREAM,
0, socket.AI_PASSIVE).AndReturn(
[(None, None, None, None, ('foo', 'bar', 'baz')),
(None, None, None, None, (1, 2, 3, 4, 5)),
(None, None, None, None, (3, 4))])
wsgi_server._SingleAddressWsgiServer(('foo', 'bar'), None).AndReturn(
failing_server)
wsgi_server._SingleAddressWsgiServer((1, 2), None).AndReturn(
starting_server)
wsgi_server._SingleAddressWsgiServer((3, 4), None).AndReturn(
another_starting_server)
starting_server.start()
failing_server.start().AndRaise(wsgi_server.BindError)
another_starting_server.start()
self.mox.ReplayAll()
self.server.start()
self.mox.VerifyAll()
self.assertItemsEqual([starting_server, another_starting_server],
self.server._servers)
示例12: test_start_all_fail_to_bind
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def test_start_all_fail_to_bind(self):
failing_server = self.mox.CreateMock(
wsgi_server._SingleAddressWsgiServer)
self.mox.StubOutWithMock(wsgi_server, '_SingleAddressWsgiServer')
self.mox.StubOutWithMock(socket, 'getaddrinfo')
socket.getaddrinfo('localhost', 123, socket.AF_UNSPEC, socket.SOCK_STREAM,
0, socket.AI_PASSIVE).AndReturn(
[(None, None, None, None, ('foo', 'bar', 'baz'))])
wsgi_server._SingleAddressWsgiServer(('foo', 'bar'), None).AndReturn(
failing_server)
failing_server.start().AndRaise(wsgi_server.BindError)
self.mox.ReplayAll()
self.assertRaises(wsgi_server.BindError, self.server.start)
self.mox.VerifyAll()
示例13: test_start_port_in_use
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def test_start_port_in_use(self):
self.mox.StubOutWithMock(socket, 'getaddrinfo')
self.mox.StubOutWithMock(self.server, 'bind')
af = object()
socktype = object()
proto = object()
socket.getaddrinfo('localhost', 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
socket.AI_PASSIVE).AndReturn(
[(af, socktype, proto, None, None)])
self.server.bind(af, socktype, proto).AndRaise(socket.error)
self.mox.ReplayAll()
self.assertRaises(wsgi_server.BindError, self.server.start)
self.mox.VerifyAll()
示例14: makeport
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def makeport(self):
'''Create a new socket and send a PORT command for it.'''
msg = "getaddrinfo returns an empty list"
sock = None
for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
sock = socket.socket(af, socktype, proto)
sock.bind(sa)
except socket.error, msg:
if sock:
sock.close()
sock = None
continue
break
示例15: makeport
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import AI_PASSIVE [as 別名]
def makeport(self):
'''Create a new socket and send a PORT command for it.'''
err = None
sock = None
for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
sock = socket.socket(af, socktype, proto)
sock.bind(sa)
except socket.error, err:
if sock:
sock.close()
sock = None
continue
break