當前位置: 首頁>>代碼示例>>Python>>正文


Python socket.has_ipv6方法代碼示例

本文整理匯總了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 
開發者ID:getavalon,項目名稱:core,代碼行數:23,代碼來源:connection.py

示例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() 
開發者ID:irmen,項目名稱:Pyro5,代碼行數:19,代碼來源:test_socketutil.py

示例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')
        ) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:18,代碼來源:test_socket.py

示例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... 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:19,代碼來源:test_socket.py

示例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. 
開發者ID:mopidy,項目名稱:mopidy-mpd,代碼行數:18,代碼來源:network.py

示例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')
        ) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:20,代碼來源:test_socket.py

示例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... 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:21,代碼來源:test_socket.py

示例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) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:20,代碼來源:test_ftplib.py

示例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() 
開發者ID:thortex,項目名稱:rpi3-webiopi,代碼行數:21,代碼來源:coap.py


注:本文中的socket.has_ipv6方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。