本文整理汇总了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
示例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
示例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)
示例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
示例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)
示例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)
示例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)
示例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")
示例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")
示例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()
示例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)
示例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
示例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
示例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")
示例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")