本文整理汇总了Python中socket.has_ipv6方法的典型用法代码示例。如果您正苦于以下问题:Python socket.has_ipv6方法的具体用法?Python socket.has_ipv6怎么用?Python socket.has_ipv6使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.has_ipv6方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _has_ipv6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def _has_ipv6(host):
""" Returns True if the system can bind an IPv6 address. """
sock = None
has_ipv6 = False
if socket.has_ipv6:
# has_ipv6 returns true if cPython was compiled with IPv6 support.
# It does not tell us if the system has IPv6 support enabled. To
# determine that we must bind to an IPv6 address.
# https://github.com/shazow/urllib3/pull/611
# https://bugs.python.org/issue658327
try:
sock = socket.socket(socket.AF_INET6)
sock.bind((host, 0))
has_ipv6 = True
except Exception:
pass
if sock:
sock.close()
return has_ipv6
示例2: testCreateUnboundSockets6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def testCreateUnboundSockets6(self):
if not has_ipv6:
pytest.skip("no ipv6 capability")
s = socketutil.create_socket(ipv6=True)
assert socket.AF_INET6 == s.family
bs = socketutil.create_bc_socket(ipv6=True)
assert socket.AF_INET6 == bs.family
with contextlib.suppress(socket.error):
host, port, _, _ = s.getsockname()
# can either fail with socket.error or return (host,0)
assert 0 == port
with contextlib.suppress(socket.error):
host, port, _, _ = bs.getsockname()
# can either fail with socket.error or return (host,0)
assert 0 == port
s.close()
bs.close()
示例3: testIPv6toString
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def testIPv6toString(self):
try:
from socket import inet_pton, AF_INET6, has_ipv6
if not has_ipv6:
self.skipTest('IPv6 not available')
except ImportError:
self.skipTest('could not import needed symbols from socket')
f = lambda a: inet_pton(AF_INET6, a)
self.assertEqual('\x00' * 16, f('::'))
self.assertEqual('\x00' * 16, f('0::0'))
self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
self.assertEqual(
'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
)
示例4: testStringToIPv6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def testStringToIPv6(self):
try:
from socket import inet_ntop, AF_INET6, has_ipv6
if not has_ipv6:
self.skipTest('IPv6 not available')
except ImportError:
self.skipTest('could not import needed symbols from socket')
f = lambda a: inet_ntop(AF_INET6, a)
self.assertEqual('::', f('\x00' * 16))
self.assertEqual('::1', f('\x00' * 15 + '\x01'))
self.assertEqual(
'aef:b01:506:1001:ffff:9997:55:170',
f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
)
# XXX The following don't test module-level functionality...
示例5: try_ipv6_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def try_ipv6_socket():
"""Determine if system really supports IPv6"""
if not socket.has_ipv6:
return False
try:
socket.socket(socket.AF_INET6).close()
return True
except OSError as exc:
logger.debug(
f"Platform supports IPv6, but socket creation failed, "
f"disabling: {exc}"
)
return False
#: Boolean value that indicates if creating an IPv6 socket will succeed.
示例6: testIPv6toString
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def testIPv6toString(self):
if not hasattr(socket, 'inet_pton'):
return # No inet_pton() on this platform
try:
from socket import inet_pton, AF_INET6, has_ipv6
if not has_ipv6:
return
except ImportError:
return
f = lambda a: inet_pton(AF_INET6, a)
self.assertEqual('\x00' * 16, f('::'))
self.assertEqual('\x00' * 16, f('0::0'))
self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
self.assertEqual(
'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
)
示例7: testStringToIPv6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def testStringToIPv6(self):
if not hasattr(socket, 'inet_ntop'):
return # No inet_ntop() on this platform
try:
from socket import inet_ntop, AF_INET6, has_ipv6
if not has_ipv6:
return
except ImportError:
return
f = lambda a: inet_ntop(AF_INET6, a)
self.assertEqual('::', f('\x00' * 16))
self.assertEqual('::1', f('\x00' * 15 + '\x01'))
self.assertEqual(
'aef:b01:506:1001:ffff:9997:55:170',
f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
)
# XXX The following don't test module-level functionality...
示例8: test_main
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def test_main():
tests = [TestFTPClass, TestTimeouts]
if socket.has_ipv6:
try:
DummyFTPServer((HOST, 0), af=socket.AF_INET6)
except socket.error:
pass
else:
tests.append(TestIPv6Environment)
if ssl is not None:
tests.extend([TestTLS_FTPClassMixin, TestTLS_FTPClass])
thread_info = test_support.threading_setup()
try:
test_support.run_unittest(*tests)
finally:
test_support.threading_cleanup(*thread_info)
示例9: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import has_ipv6 [as 别名]
def __init__(self, host, port, handler):
threading.Thread.__init__(self, name="COAPThread")
self.handler = COAPHandler(handler)
self.host = host
self.port = port
self.multicast_ip = '224.0.1.123'
if socket.has_ipv6:
address_family = socket.AF_INET6
else:
address_family = socket.AF_INET
try:
self.socket = socket.socket(address_family, socket.SOCK_DGRAM)
except:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('', port))
self.socket.settimeout(3)
self.running = True
self.start()