Commit bca5cbba authored by Denis Bilenko's avatar Denis Bilenko

add green version of getfqdn() to socket; add socket.__dns__ which lists all...

add green version of getfqdn() to socket; add socket.__dns__ which lists all dns functions (for monkey)
parent 66bc867f
......@@ -129,9 +129,6 @@ def patch_thread(threading=True, _threading_local=True):
_threading_local.local = local
dns_functions = ['getaddrinfo', 'getnameinfo', 'gethostbyname', 'gethostbyname_ex', 'gethostbyaddr']
def patch_socket(dns=True, aggressive=True):
"""Replace the standard socket object with gevent's cooperative sockets.
......@@ -140,7 +137,7 @@ def patch_socket(dns=True, aggressive=True):
from gevent import socket
items = socket.__implements__[:]
if not dns:
for function in dns_functions:
for function in socket.__dns__:
items.remove(function)
# if we patch socket.socket then create_connection is already good
items.remove('create_connection')
......@@ -151,7 +148,8 @@ def patch_socket(dns=True, aggressive=True):
def patch_dns():
patch_module('socket', items=dns_functions)
from gevent import socket
patch_module('socket', items=socket.__dns__)
def patch_ssl():
......
......@@ -33,16 +33,20 @@ as well as the constants from :mod:`socket` module are imported into this module
# standard functions and classes that this module re-implements in a gevent-aware way:
__implements__ = ['create_connection',
'getaddrinfo',
'gethostbyname',
'gethostbyname_ex',
'gethostbyaddr',
'getnameinfo',
'socket',
'SocketType',
'fromfd',
'socketpair']
__dns__ = ['getaddrinfo',
'gethostbyname',
'gethostbyname_ex',
'gethostbyaddr',
'getnameinfo',
'getfqdn']
__implements__ += __dns__
# non-standard functions that this module provides:
__extensions__ = ['wait_read',
'wait_write',
......@@ -51,7 +55,6 @@ __extensions__ = ['wait_read',
# standard functions and classes that this module re-imports
__imports__ = ['error',
'gaierror',
'getfqdn',
'herror',
'htonl',
'htons',
......@@ -658,6 +661,32 @@ def getnameinfo(sockaddr, flags):
return get_hub().resolver.getnameinfo(sockaddr, flags)
def getfqdn(name=''):
"""Get fully qualified domain name from name.
An empty argument is interpreted as meaning the local host.
First the hostname returned by gethostbyaddr() is checked, then
possibly existing aliases. In case no FQDN is available, hostname
from gethostname() is returned.
"""
name = name.strip()
if not name or name == '0.0.0.0':
name = gethostname()
try:
hostname, aliases, ipaddrs = gethostbyaddr(name)
except error:
pass
else:
aliases.insert(0, hostname)
for name in aliases:
if '.' in name:
break
else:
name = hostname
return name
try:
from gevent.ssl import sslwrap_simple as ssl, SSLError as sslerror, SSLSocket as SSLType
_have_ssl = True
......
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