'SRV resolver. Takes server=(host, port) as argument. Returns new (host, port) pair'

def srv_lookup(self, server):

    if (HAVE_DNSPYTHON or HAVE_PYDNS):
        (host, port) = server
        possible_queries = [('_xmpp-client._tcp.' + host)]
        for query in possible_queries:
            try:
                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.DiscoverNameServers()
                    response = DNS.Request().req(query, qtype='SRV')
                    answers = response.answers
                    if (len(answers) > 0):
                        (_, _, port, host) = answers[0]['data']
                        del _
                        port = int(port)
                        break
            except:
                self.DEBUG(('An    error    occurred    while    looking    up    %s' % query), 'warn')
        server = (host, port)
    else:
        self.DEBUG('Could    not    load    one    of    the    supported    DNS    libraries    (dnspython    or    pydns).    SRV    records    will    not    be    queried    and    you    may    need    to    set    custom    hostname/port    for    some    servers    to    be    accessible.\n', 'warn')
    return server
