Commit d28b2e66 authored by Jason Toffaletti's avatar Jason Toffaletti

add gethostbyname

--HG--
extra : transplant_source : %23t%1A%C0%A6%86%96%60%834%AFPB%3CG%F1ZT%16%CF
parent 67be675a
......@@ -25,15 +25,16 @@ def patch_thread():
# XXX should also patch threadlocal
def patch_socket():
from gevent.socket import GreenSocket, fromfd, wrap_ssl, socketpair, getaddrinfo, getnameinfo
from gevent.socket import GreenSocket, fromfd, wrap_ssl, socketpair, getaddrinfo, getnameinfo, gethostbyname
_socket = __import__('socket')
_socket.socket = GreenSocket
_socket.fromfd = fromfd
_socket.ssl = wrap_ssl
_socket.socketpair = socketpair
# XXX also gethostbyname, getaddrinfo
# dns
_socket.getaddrinfo = getaddrinfo
_socket.getnameinfo = getnameinfo
_socket.gethostbyname = gethostbyname
def patch_ssl():
if sys.version_info[:2] >= (2, 6):
......
......@@ -36,6 +36,8 @@ gaierror = __socket__.gaierror
import sys
import errno
import time
import random
import re
from gevent.hub import getcurrent, get_hub, spawn_raw, Waiter
from gevent import core
......@@ -646,6 +648,27 @@ def _dns_helper(result, type, ttl, addrs, args):
(waiter,) = args
waiter.switch((result, type, ttl, addrs))
_ip_re = re.compile('[\d\.]+')
def gethostbyname(hostname):
# 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 hostname == 'localhost': # hack
return '127.0.0.1'
if _ip_re.match(hostname): # hack
return hostname
waiter = Waiter()
core.dns_resolve_ipv4(hostname, core.DNS_QUERY_NO_SEARCH, _dns_helper, waiter)
result, type, ttl, addrs = waiter.wait()
if result != core.DNS_ERR_NONE:
# hack to make testSockName pass
# should use /etc/hosts
if hostname == __socket__.gethostname():
return '0.0.0.0'
raise gaierror(result)
return random.choice(addrs)
def getaddrinfo(host, port, family=__socket__.AF_INET, socktype=__socket__.SOCK_STREAM, proto=0, flags=0):
waiter = Waiter()
if family == __socket__.AF_INET:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment