How to use the twisted.internet.defer.Deferred function in Twisted

To help you get started, we’ve selected a few Twisted examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github buildbot / buildbot / buildbot / broken_test / runutils.py View on Github external
def myGetProcessOutputAndValue(executable, args=(), env={}, path='.',
                               _reactor_ignored=None, stdin=None):
    """Like twisted.internet.utils.getProcessOutputAndValue but takes
    stdin, too."""
    d = defer.Deferred()
    p = _PutEverythingGetter(d, stdin)
    reactor.spawnProcess(p, executable, (executable,)+tuple(args), env, path)
    return d
github twisted / twisted / sandbox / z3p / test_simpleconch.py View on Github external
if os.name != 'posix':
            raise unittest.SkipTest("cannot run on non-posix") # why?
        global theTest
        theTest = self
        realm = ConchTestRealm()
        p = portal.Portal(realm)
        sshpc = ConchTestSSHChecker()
        sshpc.registerChecker(ConchTestPasswordChecker())
        sshpc.registerChecker(ConchTestPublicKeyChecker())
        p.registerChecker(sshpc)
        self.fac = fac = SSHTestFactory()
        fac.portal = p
        theTest.fac = fac
        self.server = reactor.listenTCP(0, fac, interface="127.0.0.1")
        port = self.server.getHost()[2]
        d = defer.Deferred()
        cc = protocol.ClientCreator(reactor, simpleconch.SimpleTransport, d)
        d.addCallback(self._cbSimpleConnected)
        d.addErrback(self._ebFailTest, "failed before connection")
        def _failTest():
            reactor.crash()
            self.fail('test took too long') # logged but caught by reactor
        self.sessions = []
        timeout = reactor.callLater(10, _failTest)
        d = cc.connectTCP('localhost', port)
        d.addErrback(self._ebFailTest)
        reactor.run()
        # test finished.. might have passed, might have failed. Must cleanup.
        fac.proto.done = 1
        fac.proto.transport.loseConnection()
        reactor.iterate()
        reactor.iterate()
github apple / ccs-calendarserver / twext / enterprise / adbapi2.py View on Github external
def _enspool(self, cmd, a=(), kw={}):
        d = Deferred()
        self._spool.append((d, cmd, a, kw))
        return d
github graphite-project / carbon / lib / carbon / client.py View on Github external
def queueSpaceCallback(self, result):
    if self.queueFull.called:
      log.clients('%s send queue has space available' % self.connectedProtocol)
      self.queueFull = Deferred()
      self.queueFull.addCallbacks(self.queueFullCallback, log.err)
      state.events.cacheSpaceAvailable()
    self.queueHasSpace = Deferred()
    self.queueHasSpace.addCallbacks(self.queueSpaceCallback, log.err)
github cowrie / cowrie / src / cowrie / ssh / userauth.py View on Github external
resp.append((message, 0))
            elif kind == 2:  # Text
                resp.append((message, 1))
            elif kind in (3, 4):
                return defer.fail(error.ConchError(
                    'cannot handle PAM 3 or 4 messages'))
            else:
                return defer.fail(error.ConchError(
                    'bad PAM auth kind %i' % (kind,)))
        packet = NS(b'') + NS(b'') + NS(b'')
        packet += struct.pack('>L', len(resp))
        for prompt, echo in resp:
            packet += NS(prompt)
            packet += chr(echo)
        self.transport.sendPacket(userauth.MSG_USERAUTH_INFO_REQUEST, packet)
        self._pamDeferred = defer.Deferred()
        return self._pamDeferred
github twisted / twisted / docs / core / examples / echoserv_ssl.py View on Github external
def main(reactor):
    log.startLogging(sys.stdout)
    certData = getModule(__name__).filePath.sibling('server.pem').getContent()
    certificate = ssl.PrivateCertificate.loadPEM(certData)
    factory = protocol.Factory.forProtocol(echoserv.Echo)
    reactor.listenSSL(8000, factory, certificate.options())
    return defer.Deferred()
github AnemoneLabs / unmessage / unmessage / untalk.py View on Github external
def send_data(self, data):
        d = Deferred()

        def send():
            try:
                self.connection.send(data)
            except Exception as e:
                d.errback(errors.UntalkError(
                    message='{}: {}'.format(str(type(e)), e.message)))
            else:
                d.callback(None)

        t = Thread(target=send)
        t.daemon = True
        t.start()

        return d
github twisted / twisted / sandbox / livejournal.py View on Github external
def __init__(self):
        self.d = defer.Deferred()
github etingof / pysnmp / pysnmp / hlapi / v3arch / twisted / cmdgen.py View on Github external
varBindTableRow, lookupMib)
                    for varBindTableRow in varBindTable
                ]

            except Exception as e:
                deferred.errback(Failure(e))

            else:
                deferred.callback((errorStatus, errorIndex, varBindTable))

    addrName, paramsName = LCD.configure(
        snmpEngine, authData, transportTarget, contextData.contextName)

    varBinds = VB_PROCESSOR.makeVarBinds(snmpEngine.cache, varBinds)

    deferred = Deferred()

    cmdgen.NextCommandGenerator().sendVarBinds(
        snmpEngine, addrName, contextData.contextEngineId,
        contextData.contextName, varBinds, __cbFun,
        (options.get('lookupMib', True), deferred))

    return deferred
github ralphm / wokkel / wokkel / server.py View on Github external
def initialize(self):
        self._deferred = defer.Deferred()
        self.xmlstream.addObserver(xmlstream.STREAM_ERROR_EVENT,
                                   self.onStreamError)
        self.xmlstream.addObserver("/verify[@xmlns='%s']" % NS_DIALBACK,
                                   self.onVerify)

        verify = domish.Element((NS_DIALBACK, 'verify'))
        verify['from'] = self.thisHost
        verify['to'] = self.otherHost
        verify['id'] = self.originalStreamID
        verify.addContent(self.key)

        self.xmlstream.send(verify)
        return self._deferred