def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
python类gethostbyname()的实例源码
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def connect(self, address):
if isinstance(address, tuple) and len(address) == 2:
address = gethostbyname(address[0]), address[1]
if self.timeout == 0.0:
return self._sock.connect(address)
sock = self._sock
if self.timeout is None:
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
wait_readwrite(sock.fileno(), event=self._rw_event)
else:
raise error(result, strerror(result))
else:
end = time.time() + self.timeout
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
timeleft = end - time.time()
if timeleft <= 0:
raise timeout('timed out')
wait_readwrite(sock.fileno(), timeout=timeleft, event=self._rw_event)
else:
raise error(result, strerror(result))
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))
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def connect(self, address):
if isinstance(address, tuple) and len(address) == 2:
address = gethostbyname(address[0]), address[1]
if self.timeout == 0.0:
return self._sock.connect(address)
sock = self._sock
if self.timeout is None:
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
wait_readwrite(sock.fileno(), event=self._rw_event)
else:
raise error(result, strerror(result))
else:
end = time.time() + self.timeout
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
timeleft = end - time.time()
if timeleft <= 0:
raise timeout('timed out')
wait_readwrite(sock.fileno(), timeout=timeleft, event=self._rw_event)
else:
raise error(result, strerror(result))
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))
def gethostbyname(self, *args):
return self.pool.apply(_socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply(_socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def connect(self, address):
if isinstance(address, tuple) and len(address) == 2:
address = gethostbyname(address[0]), address[1]
if self.timeout == 0.0:
return self._sock.connect(address)
sock = self._sock
if self.timeout is None:
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
wait_readwrite(sock.fileno(), event=self._rw_event)
else:
raise error(result, strerror(result))
else:
end = time.time() + self.timeout
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
timeleft = end - time.time()
if timeleft <= 0:
raise timeout('timed out')
wait_readwrite(sock.fileno(), timeout=timeleft, event=self._rw_event)
else:
raise error(result, strerror(result))
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))
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply(_socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def gethostbyname(self, *args):
return self.pool.apply_e(self.expected_errors, _socket.gethostbyname, args)
def connect(self, address):
if isinstance(address, tuple) and len(address) == 2:
address = gethostbyname(address[0]), address[1]
if self.timeout == 0.0:
return self._sock.connect(address)
sock = self._sock
if self.timeout is None:
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
wait_readwrite(sock.fileno(), event=self._rw_event)
else:
raise error(result, strerror(result))
else:
end = time.time() + self.timeout
while True:
err = sock.getsockopt(SOL_SOCKET, SO_ERROR)
if err:
raise error(err, strerror(err))
result = sock.connect_ex(address)
if not result or result == EISCONN:
break
elif (result in (EWOULDBLOCK, EINPROGRESS, EALREADY)) or (result == EINVAL and is_windows):
timeleft = end - time.time()
if timeleft <= 0:
raise timeout('timed out')
wait_readwrite(sock.fileno(), timeout=timeleft, event=self._rw_event)
else:
raise error(result, strerror(result))
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))
def gethostbyname(self, *args):
return self.pool.apply(_socket.gethostbyname, args)