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


Python _socket.gaierror方法代碼示例

本文整理匯總了Python中_socket.gaierror方法的典型用法代碼示例。如果您正苦於以下問題:Python _socket.gaierror方法的具體用法?Python _socket.gaierror怎麽用?Python _socket.gaierror使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在_socket的用法示例。


在下文中一共展示了_socket.gaierror方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: resolve_hostnames

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def resolve_hostnames(hostnames, timeout):
    """
    Do DNS resolution for a given list of hostnames

    This function uses gevent to resolve all the hostnames in *parallel*

    Args:
        hostnames (list): A list of strings
        timeout (int): The number of seconds to wait for resolution of **all** hostnames

    Returns:
        list: A list of (hostname, address) tuples in the same order as the input list of hostnames

    """
    assert validators.PanoptesValidators.valid_nonempty_iterable_of_strings(hostnames), u'hostnames should be a list'
    assert validators.PanoptesValidators.valid_nonzero_integer(timeout), u'timeout should be an int greater than zero'

    jobs = [gevent.spawn(wrap_errors(gaierror, socket.gethostbyname), host) for host in hostnames]
    gevent.joinall(jobs, timeout=timeout)
    addresses = [job.value if not isinstance(job.get(), gaierror) else None for job in jobs]
    results = [(hostnames[i], result) for i, result in enumerate(addresses)]
    return results 
開發者ID:yahoo,項目名稱:panoptes,代碼行數:24,代碼來源:helpers.py

示例2: get_hostnames

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def get_hostnames(ips, timeout):
    """
    Do DNS resolution for a given list of IPs

    Args:
        ips (list): A list of IPs
        timeout (int): The number of seconds to wait for resolution of **all** IPs

    Returns:
        list: A list of (address, hosname) tuples in the same order as the input list of IPs
    """
    assert validators.PanoptesValidators.valid_nonempty_iterable_of_strings(ips), u'ips should be a list'
    assert validators.PanoptesValidators.valid_nonzero_integer(timeout), u'timeout should be an int greater than zero'

    jobs = [gevent.spawn(wrap_errors((gaierror, herror), socket.gethostbyaddr), ip) for ip in ips]
    gevent.joinall(jobs, timeout=timeout)
    hostnames = [None if isinstance(job.get(), (gaierror, herror)) else job.value for job in jobs]
    results = {
        ips[i]: unknown_hostname(ips[i]) if ((not result) or
                                             (not result[0]) or
                                             result[0].startswith(u'UNKNOWN'))
        else result[0]
        for i, result in enumerate(hostnames)}
    return results 
開發者ID:yahoo,項目名稱:panoptes,代碼行數:26,代碼來源:helpers.py

示例3: test_getnameinfo

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def test_getnameinfo(self):
        '''Tests _socket.getnameinfo()'''
        #sanity
        _socket.getnameinfo(("127.0.0.1", 80), 8)
        _socket.getnameinfo(("127.0.0.1", 80), 9)
            
        host, service = _socket.getnameinfo( ("127.0.0.1", 80), 8)
        self.assertEqual(service, '80')
            
        host, service = _socket.getnameinfo( ("127.0.0.1", 80), 0)
        self.assertEqual(service, "http")
        #IP gives a TypeError
        #self.assertRaises(SystemError, _socket.getnameinfo, ("127.0.0.1"), 8)
        #self.assertRaises(SystemError, _socket.getnameinfo, (321), 8)
        self.assertRaises(TypeError, _socket.getnameinfo, ("127.0.0.1"), '0')
        self.assertRaises(TypeError, _socket.getnameinfo, ("127.0.0.1", 80, 0, 0, 0), 8)
        self.assertRaises(_socket.gaierror, _socket.getnameinfo, ('no such host will ever exist', 80), 8) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:19,代碼來源:test__socket.py

示例4: query

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def query(self, name, dnsclass, type_):
        if isinstance(name, unicode):
            name = name.encode('ascii')
        elif not isinstance(name, str):
            raise TypeError('Expected string, not %s' % type(name).__name__)

        while True:
            ares = self.ares
            try:
                waiter = Waiter(self.hub)
                ares.query(waiter, name, dnsclass, type_)
                result = waiter.get()

                return result

            except gaierror:
                if ares is self.ares:
                    raise 
開發者ID:PaloAltoNetworks,項目名稱:minemeld-core,代碼行數:20,代碼來源:dig.py

示例5: gethostbyname_ex

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def gethostbyname_ex(self, hostname, family=AF_INET):
        if PY3:
            if isinstance(hostname, str):
                hostname = hostname.encode('idna')
            elif not isinstance(hostname, (bytes, bytearray)):
                raise TypeError('Expected es(idna), not %s' % type(hostname).__name__)
        else:
            if isinstance(hostname, text_type):
                hostname = hostname.encode('ascii')
            elif not isinstance(hostname, str):
                raise TypeError('Expected string, not %s' % type(hostname).__name__)

        while True:
            ares = self.ares
            try:
                waiter = Waiter(self.hub)
                ares.gethostbyname(waiter, hostname, family)
                result = waiter.get()
                if not result[-1]:
                    raise gaierror(-5, 'No address associated with hostname')
                return result
            except gaierror:
                if ares is self.ares:
                    raise
                # "self.ares is not ares" means channel was destroyed (because we were forked) 
開發者ID:leancloud,項目名稱:satori,代碼行數:27,代碼來源:resolver_ares.py

示例6: test_getnameinfo

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def test_getnameinfo(self):
        '''Tests _socket.getnameinfo()'''
        #sanity
        _socket.getnameinfo(("127.0.0.1", 80), 8)
        _socket.getnameinfo(("127.0.0.1", 80), 9)

        host, service = _socket.getnameinfo( ("127.0.0.1", 80), 8)
        self.assertEqual(service, '80')

        host, service = _socket.getnameinfo( ("127.0.0.1", 80), 0)
        self.assertEqual(service, "http")
        #IP gives a TypeError
        #self.assertRaises(SystemError, _socket.getnameinfo, ("127.0.0.1"), 8)
        #self.assertRaises(SystemError, _socket.getnameinfo, (321), 8)
        self.assertRaises(TypeError, _socket.getnameinfo, ("127.0.0.1"), '0')
        self.assertRaises(TypeError, _socket.getnameinfo, ("127.0.0.1", 80, 0, 0, 0), 8)
        self.assertRaises(_socket.gaierror, _socket.getnameinfo, ('no such host will ever exist', 80), 8) 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:19,代碼來源:test__socket.py

示例7: _open_channel

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def _open_channel(self, host, port, username, password, connect_timeout):
        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        try:
            self.client.connect(host,
                                port=port,
                                username=username,
                                password=password,
                                timeout=connect_timeout,
                                allow_agent=False,
                                look_for_keys=False)
        except timeout:
            raise ConnectTimeout(host, port)
        except gaierror:
            raise CouldNotConnect(host, port)

        self.channel = self.client.invoke_shell()

        self._wait_for(self.prompt) 
開發者ID:internap,項目名稱:netman,代碼行數:21,代碼來源:ssh.py

示例8: test_gethostbyname

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def test_gethostbyname(self):
        '''Tests _socket.gethostbyname'''
        #sanity
        self.assertEqual(_socket.gethostbyname("localhost"), "127.0.0.1")
        self.assertEqual(_socket.gethostbyname("127.0.0.1"), "127.0.0.1")
        self.assertEqual(_socket.gethostbyname("<broadcast>"), "255.255.255.255")
        
        #negative
        self.assertRaises(_socket.gaierror, _socket.gethostbyname, "should never work") 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:11,代碼來源:test__socket.py

示例9: test_gethostbyname_ex

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def test_gethostbyname_ex(self):
        '''Tests _socket.gethostbyname_ex'''
        #sanity
        joe = _socket.gethostbyname_ex("localhost")[2]
        self.assertTrue("127.0.0.1" in joe)
        joe = _socket.gethostbyname_ex("127.0.0.1")[2]
        self.assertTrue("127.0.0.1" in joe)
        
        #negative
        self.assertRaises(_socket.gaierror, _socket.gethostbyname_ex, "should never work") 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:12,代碼來源:test__socket.py

示例10: _get_addr_info

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def _get_addr_info(self, host, family):
        self.logger.debug("Resolving %s", host)
        try:
            info = getaddrinfo(host, 0, family, SOCK_STREAM)
            return set(map(itemgetter(0), map(itemgetter(-1), info)))
        except gaierror as e:
            self.logger.error("Failed to resolve %s: %s", host, e)
            return set() 
開發者ID:OpenMTC,項目名稱:OpenMTC,代碼行數:10,代碼來源:wsgi.py

示例11: __init__

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def __init__(self, hub=None):
        if hub is None:
            hub = get_hub()
        self.pool = hub.threadpool
        if _socket.gaierror not in hub.NOT_ERROR:
            # Do not cause lookup failures to get printed by the default
            # error handler. This can be very noisy.
            hub.NOT_ERROR += (_socket.gaierror, _socket.herror) 
開發者ID:leancloud,項目名稱:satori,代碼行數:10,代碼來源:resolver_thread.py

示例12: getaddrinfo

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def getaddrinfo(self, host, port, family=0, socktype=0, proto=0, flags=0):
        while True:
            ares = self.ares
            try:
                return self._getaddrinfo(host, port, family, socktype, proto, flags)
            except gaierror:
                if ares is self.ares:
                    raise 
開發者ID:leancloud,項目名稱:satori,代碼行數:10,代碼來源:resolver_ares.py

示例13: gethostbyaddr

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def gethostbyaddr(self, ip_address):
        ip_address = _resolve_special(ip_address, AF_UNSPEC)
        while True:
            ares = self.ares
            try:
                return self._gethostbyaddr(ip_address)
            except gaierror:
                if ares is self.ares:
                    raise 
開發者ID:leancloud,項目名稱:satori,代碼行數:11,代碼來源:resolver_ares.py

示例14: test_gethostbyname

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def test_gethostbyname(self):
        '''Tests _socket.gethostbyname'''
        #sanity
        self.assertEqual(_socket.gethostbyname("localhost"), "127.0.0.1")
        self.assertEqual(_socket.gethostbyname("127.0.0.1"), "127.0.0.1")
        self.assertEqual(_socket.gethostbyname("<broadcast>"), "255.255.255.255")

        #negative
        self.assertRaises(_socket.gaierror, _socket.gethostbyname, "should never work") 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:11,代碼來源:test__socket.py

示例15: test_gethostbyname_ex

# 需要導入模塊: import _socket [as 別名]
# 或者: from _socket import gaierror [as 別名]
def test_gethostbyname_ex(self):
        '''Tests _socket.gethostbyname_ex'''
        #sanity
        joe = _socket.gethostbyname_ex("localhost")[2]
        self.assertIn("127.0.0.1" , joe)
        joe = _socket.gethostbyname_ex("127.0.0.1")[2]
        self.assertIn("127.0.0.1", joe)

        #negative
        self.assertRaises(_socket.gaierror, _socket.gethostbyname_ex, "should never work") 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:12,代碼來源:test__socket.py


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