def doconnect(self): """ connect to the server. """ target = None port = None try: dns = getdns() import dns.resolver except: pass else: try: xmpp_srv = "_xmpp-client._tcp.%s" % (self.cfg.server or self.cfg.host) answers = dns.resolver.query(xmpp_srv, dns.rdatatype.SRV) except ImportError: loging.warn("resolver is not available.") except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): logging.info("no appropriate SRV record found.") else: addresses = {} intmax = 0 for answer in answers: try: intmax += answer.priority addresses[intmax] = (answer.target.to_text()[:-1], answer.port) except Exception, ex: logging.debug("%s - skipping %s: %s" % (self.cfg.name, str(answer), str(ex))) priorities = [x for x in addresses.keys()] priorities.sort() picked = random.randint(0, intmax) for priority in priorities: if picked <= priority: target = addresses[priority] break if not target or self.cfg.noresolver: target = (self.cfg.server or self.cfg.host, self.cfg.port) logging.debug("%s - TARGET is %s" % (self.cfg.name, target)) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setblocking(0) self.sock.settimeout(30) logging.warn("connecting to %s (%s)" % (target, self.cfg.name)) self.sock.connect(target) self.target = target logging.warn("connected! trying to login as %s (%s)" % (self.cfg.user, self.cfg.name)) return True