[dman] use dnspython if available, or switch to pydns
This commit is contained in:
parent
bb7e4079cd
commit
59464c6b4b
|
@ -32,10 +32,19 @@ from simplexml import ustr
|
||||||
from client import PlugIn
|
from client import PlugIn
|
||||||
from protocol import *
|
from protocol import *
|
||||||
|
|
||||||
|
# determine which DNS resolution library is available
|
||||||
|
HAVE_DNSPYTHON = False
|
||||||
|
HAVE_PYDNS = False
|
||||||
try:
|
try:
|
||||||
import dns.resolver
|
import dns.resolver # http://dnspython.org/
|
||||||
except:
|
HAVE_DNSPYTHON = True
|
||||||
pass
|
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_RECEIVED='DATA RECEIVED'
|
||||||
DATA_SENT='DATA SENT'
|
DATA_SENT='DATA SENT'
|
||||||
|
@ -60,20 +69,28 @@ class TCPsocket(PlugIn):
|
||||||
self._exported_methods=[self.send,self.disconnect]
|
self._exported_methods=[self.send,self.disconnect]
|
||||||
|
|
||||||
# SRV resolver
|
# SRV resolver
|
||||||
if 'dns' in globals(): # if dnspython is available support SRV
|
if HAVE_DNSPYTHON or HAVE_PYDNS :
|
||||||
host, port = server
|
host, port = server
|
||||||
possible_queries = ['_xmpp-client._tcp.' + host]
|
possible_queries = ['_xmpp-client._tcp.' + host]
|
||||||
|
|
||||||
for query in possible_queries:
|
for query in possible_queries:
|
||||||
try:
|
try:
|
||||||
answers = [x for x in dns.resolver.query(query, 'SRV')]
|
if HAVE_DNSPYTHON :
|
||||||
if answers:
|
answers = [x for x in dns.resolver.query(query, 'SRV')]
|
||||||
host = str (answers[0].target)
|
if answers:
|
||||||
port = int (answers[0].port)
|
host = str (answers[0].target)
|
||||||
break
|
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:
|
except:
|
||||||
pass
|
self.DEBUG('An error occurred looking up %s' % query, 'warn')
|
||||||
|
|
||||||
server = (host, port)
|
server = (host, port)
|
||||||
# end of SRV resolver
|
# end of SRV resolver
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue