def connect(self, host=None, port=None, use_ssl=False, use_tls=False, reattempt=True): """Connect to the server. Setting ``reattempt`` to ``True`` will cause connection attempts to be made every second until a successful connection is established. :param host: The name of the desired server for the connection. Defaults to :attr:`server_host`. :param port: Port to connect to on the server. Defauts to :attr:`server_port`. :param use_ssl: Flag indicating if SSL should be used by connecting directly to a port using SSL. :param use_tls: Flag indicating if TLS should be used, allowing for connecting to a port without using SSL immediately and later upgrading the connection. :param reattempt: Flag indicating if the socket should reconnect after disconnections. """ if host is None: host = self.server_host if port is None: port = self.server_port self.server_name = self.boundjid.host if use_tls: log.info("XEP-0114 components can not use TLS") log.debug("Connecting to %s:%s", host, port) return XMLStream.connect(self, host=host, port=port, use_ssl=use_ssl, use_tls=False, reattempt=reattempt)
def connect(self, address=tuple(), reattempt=True, use_tls=True, use_ssl=False): """Connect to the XMPP server. When no address is given, a SRV lookup for the server will be attempted. If that fails, the server user in the JID will be used. :param address -- A tuple containing the server's host and port. :param reattempt: If ``True``, repeat attempting to connect if an error occurs. Defaults to ``True``. :param use_tls: Indicates if TLS should be used for the connection. Defaults to ``True``. :param use_ssl: Indicates if the older SSL connection method should be used. Defaults to ``False``. """ self.session_started_event.clear() if not self.server and len(address): # we used anonymous authentication self.stream_header = """<stream:stream to='%s' xmlns:stream='http://etherx.jabber.org/streams' xmlns='%s' version='1.0'>""" % (address[0],self.default_ns) if not address: address = (self.boundjid.host, 5222) return XMLStream.connect(self, address[0], address[1], use_tls=use_tls, use_ssl=use_ssl, reattempt=reattempt)
def connect(self, address=tuple(), reattempt=True, use_tls=True, use_ssl=False): """Connect to the XMPP server. When no address is given, a SRV lookup for the server will be attempted. If that fails, the server user in the JID will be used. :param address -- A tuple containing the server's host and port. :param reattempt: If ``True``, repeat attempting to connect if an error occurs. Defaults to ``True``. :param use_tls: Indicates if TLS should be used for the connection. Defaults to ``True``. :param use_ssl: Indicates if the older SSL connection method should be used. Defaults to ``False``. """ self.session_started_event.clear() # If an address was provided, disable using DNS SRV lookup; # otherwise, use the domain from the client JID with the standard # XMPP client port and allow SRV lookup. if address: self.dns_service = None else: address = (self.boundjid.host, 5222) self.dns_service = 'xmpp-client' self._expected_server_name = self.boundjid.host return XMLStream.connect(self, address[0], address[1], use_tls=use_tls, use_ssl=use_ssl, reattempt=reattempt)
def connect(self, address=tuple(), reattempt=True, use_tls=True, use_ssl=False): """ Connect to the XMPP server. When no address is given, a SRV lookup for the server will be attempted. If that fails, the server user in the JID will be used. Arguments: address -- A tuple containing the server's host and port. reattempt -- If True, reattempt the connection if an error occurs. Defaults to True. use_tls -- Indicates if TLS should be used for the connection. Defaults to True. use_ssl -- Indicates if the older SSL connection method should be used. Defaults to False. """ self.session_started_event.clear() if not address: address = (self.boundjid.host, 5222) return XMLStream.connect(self, address[0], address[1], use_tls=use_tls, use_ssl=use_ssl, reattempt=reattempt)
def connect(self, address=tuple(), reattempt=True, use_tls=True): """ Connect to the XMPP server. When no address is given, a SRV lookup for the server will be attempted. If that fails, the server user in the JID will be used. Arguments: address -- A tuple containing the server's host and port. reattempt -- If True, reattempt the connection if an error occurs. Defaults to True. use_tls -- Indicates if TLS should be used for the connection. Defaults to True. """ self.session_started_event.clear() if not address or len(address) < 2: if not self.srv_support: log.debug("Did not supply (address, port) to connect" + \ " to and no SRV support is installed" + \ " (http://www.dnspython.org)." + \ " Continuing to attempt connection, using" + \ " server hostname from JID.") else: log.debug("Since no address is supplied," + \ "attempting SRV lookup.") try: xmpp_srv = "_xmpp-client._tcp.%s" % self.boundjid.host answers = dns.resolver.query(xmpp_srv, dns.rdatatype.SRV) except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): log.debug("No appropriate SRV record found." + \ " Using JID server name.") except (dns.exception.Timeout,): log.debug("DNS resolution timed out.") else: # Pick a random server, weighted by priority. addresses = {} intmax = 0 for answer in answers: intmax += answer.priority addresses[intmax] = (answer.target.to_text()[:-1], answer.port) #python3 returns a generator for dictionary keys priorities = [x for x in addresses.keys()] priorities.sort() picked = random.randint(0, intmax) for priority in priorities: if picked <= priority: address = addresses[priority] break if not address: # If all else fails, use the server from the JID. address = (self.boundjid.host, 5222) return XMLStream.connect(self, address[0], address[1], use_tls=use_tls, reattempt=reattempt)
def connect(self): """ Connect to the server. Overrides XMLStream.connect. """ log.debug("Connecting to %s:%s" % (self.server_host, self.server_port)) return XMLStream.connect(self, self.server_host, self.server_port)
def connect(self, address=tuple()): """ Connect to the XMPP server. When no address is given, a SRV lookup for the server will be attempted. If that fails, the server user in the JID will be used. Arguments: address -- A tuple containing the server's host and port. """ self.session_started_event.clear() if not address or len(address) < 2 and not self.srv_support: log.debug("Did not supply (address, port) to connect" + \ " to and no SRV support is installed" + \ " (http://www.dnspython.org)." + \ " Continuing to attempt connection, using" + \ " server hostname from JID.") if not address: # If all else fails, use the server from the JID. address = (self.boundjid.host, 5222) return XMLStream.connect(self, address[0], address[1], use_tls=True)
log = logging.getLogger('sleekxmpp') log.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) formatter = logging.Formatter('%(message)s') ch.setFormatter(formatter) log.addHandler(ch) def bbb(*args): def __init__(self): print(args) stream = XMLStream() stream.response_timeout = 15 stream.connect(host = '147.102.6.34', port = 5222) print(stream.new_id()) register_stanza_plugin(Iq, Registration) iq = Iq(stream=stream, stype='set') iq['to'] = 'gic' iq['register']['username'] = '******' iq['register']['password'] = '******' print('iq.send = ' + str(iq.send()))