Exemplo n.º 1
0
 def inboundConnect(self, address, password='******',
         protocol=EventSocket, factory=EventSocketClientFactory,
         subscribers=[]):
     self.inbound_factory = factory(password=password, notifyTarget=self)
     self.inbound_factory.protocol = protocol
     if self not in subscribers:
         subscribers.append(self)
     self.auto_subscribers = subscribers
     self.address = address
     host, port = utils.parse_host_port(address, 8021)
     reactor.connectTCP(host, port, self.inbound_factory)
Exemplo n.º 2
0
    def connect(self, server_address, service=None, protocol='tcp'):
        """
        Connect to the given server_address.

        Without the 'service' keyword, the server_address can be formatted
        as HOST[:PORT] (if no port given, 8800 will be assumed).

        If 'service' is used, it must be the name of a service to look up
        using a DNS SRV record at the server_address (in this case, no port is
        expected in the server_address). The 'protocol' is also sent used in
        the DNS SRV lookup.

        The 'protocol' keyword is ignored if 'service' is not used.

        Examples:
            c = Client()

            # connect to example.com at port 8800
            c.connect('example.com')

            # connect to example.com at port 45
            c.connect('example.com:45')

            # look up the host and port using SRV, passing 'SIP' as the
            ## service name to the DNS SRV host at example.com
            c.connect('example.com', service='SIP')

         TODO: Twisted uses it's own lookup cache that appears to be
         cleared when the process terminates. I am unsure whether that
         cache respects SRV TTL; if not, long-living reconnecting
         clients *might* not get a new lookup. Further testing is needed
         to determine this.
        """
        self.ip = server_address
        if service:
            connector = SRVConnector(reactor, service, server_address,
                self.factory, protocol=protocol)
            connector.connect()
        else:
            self.host, self.port = utils.parse_host_port(server_address, 8800)
            try:
                reactor.connectTCP(self.host, self.port, self.factory)
            except error.ConnectionRefusedError, e:
                logger.error(e)
                # wraps the error in a slightly more generic ClientError
                ## and reraises
                raise ClientError(str(e))
Exemplo n.º 3
0
    def connectSSL(self, server_address, cert_path, cert_chain_path=None,
            service=None, protocol='ssl'):
        """
        Connect to the given server_address.

        See the docstring for Client.connect() for more information.
        """
        self.ip = server_address

        # verify that the given key/cert files actually exist
        if not os.path.exists(cert_path):
            raise ClientError('Cert file %r does not exist!' % cert_path)
        if cert_chain_path and not os.path.exists(cert_chain_path):
            raise ClientError('Cert chain file %r does not exist!' % \
                cert_chain_path)

        class CtxFactory(ssl.ClientContextFactory):
            def getContext(self):
                self.method = SSL.SSLv23_METHOD
                ctx = ssl.ClientContextFactory.getContext(self)
                if cert_chain_path:
                    ctx.use_certificate_chain_file(cert_chain_path)
                ctx.use_certificate_file(cert_path)
                return ctx

        if service:
            connector = SRVConnector(reactor, service, server_address,
                self.factory, protocol=protocol, connectFuncName='connectSSL',
                connectFuncArgs=(CtxFactory(),))
            connector.connect()
        else:
            self.host, self.port = utils.parse_host_port(server_address, 2220)
            try:
                reactor.connectSSL(self.host, self.port, self.factory,
                    CtxFactory())
            except error.ConnectionRefusedError, e:
                logger.error(e)
                # wraps the error in a slightly more generic ClientError
                ## and reraises
                raise ClientError(str(e))