[dman] use dnspython if available, or switch to pydns

This commit is contained in:
Yann Leboulanger 2005-09-05 06:48:56 +00:00
parent bb7e4079cd
commit 59464c6b4b
1 changed files with 28 additions and 11 deletions

View File

@ -32,10 +32,19 @@ from simplexml import ustr
from client import PlugIn
from protocol import *
# determine which DNS resolution library is available
HAVE_DNSPYTHON = False
HAVE_PYDNS = False
try:
import dns.resolver
except:
pass
import dns.resolver # http://dnspython.org/
HAVE_DNSPYTHON = True
except ImportError :
try :
import DNS # http://pydns.sf.net/
HAVE_PYDNS = True
except ImportError :
self.DEBUG("Couldn't load a supported DNS library.", 'warn')
"SRV records will not be queried and some servers may not be accessible.")
DATA_RECEIVED='DATA RECEIVED'
DATA_SENT='DATA SENT'
@ -60,20 +69,28 @@ class TCPsocket(PlugIn):
self._exported_methods=[self.send,self.disconnect]
# SRV resolver
if 'dns' in globals(): # if dnspython is available support SRV
if HAVE_DNSPYTHON or HAVE_PYDNS :
host, port = server
possible_queries = ['_xmpp-client._tcp.' + host]
for query in possible_queries:
try:
answers = [x for x in dns.resolver.query(query, 'SRV')]
if answers:
host = str (answers[0].target)
port = int (answers[0].port)
break
if HAVE_DNSPYTHON :
answers = [x for x in dns.resolver.query(query, 'SRV')]
if answers:
host = str (answers[0].target)
port = int (answers[0].port)
break
elif HAVE_PYDNS :
DNS.ParseResolvConf() # ensure we haven't cached an old configuration
response = DNS.Request().req(query, qtype='SRV')
answers = response.answers
if len(answers) > 0 :
_, _, port, host = answers[0]['data'] # ignore the priority and weight for now
port = int(port)
break
except:
pass
self.DEBUG('An error occurred looking up %s' % query, 'warn')
server = (host, port)
# end of SRV resolver