Ejemplo n.º 1
0
    def connect(self, host, port, username=None, password=None, retry=False):
        if retry:
            # Launch a client
            self.pbClientFactory = ReconnectingPBClientFactory()
            self.pbClientFactory.gotPerspective = self._connected
            self.pbClientFactory.disconnected = self._disconnected

            # Start login
            if username is None and password is None:
                self.pbClientFactory.startLogin(
                    Anonymous())
            else:
                self.pbClientFactory.startLogin(
                    UsernamePassword(
                        username,
                        password))

            reactor.connectTCP(host, port, self.pbClientFactory)
        else:
            # Launch a client
            self.pbClientFactory = pb.PBClientFactory()
            reactor.connectTCP(host, port, self.pbClientFactory)

            yield self.pbClientFactory.getRootObject()

            if username is None and password is None:
                yield self.pbClientFactory.login(
                    Anonymous()).addCallback(self._connected)
            else:
                yield self.pbClientFactory.login(
                    UsernamePassword(
                        username,
                        password)).addCallback(self._connected)