def gethostbyname(hostname):
""":func:`socket.gethostbyname` implemented using :mod:`gevent.dns`.
Differs in the following ways:
* raises :class:`DNSError` (a subclass of :class:`socket.gaierror`) with dns error
codes instead of standard socket error codes
* does not support ``/etc/hosts`` but calls the original :func:`socket.gethostbyname`
if *hostname* has no dots
* does not iterate through all addresses, instead picks a random one each time
"""
# TODO: this is supposed to iterate through all the addresses
# could use a global dict(hostname, iter)
# - fix these nasty hacks for localhost, ips, etc.
if not isinstance(hostname, str) or '.' not in hostname:
return _socket.gethostbyname(hostname)
if _ip4_re.match(hostname):
return hostname
if hostname == _socket.gethostname():
return _socket.gethostbyname(hostname)
addrs = None
try:
_ttl, addrs = resolve_ipv4(hostname)
except:
_ttl, addrs = resolve_ipv6(hostname)
return inet_ntop(AF_INET6, random.choice(addrs))
else:
return inet_ntop(AF_INET, random.choice(addrs))
评论列表
文章目录