Python twisted.internet() Examples

The following are 30 code examples of twisted.internet(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted , or try the search function .
Example #1
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_none_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                return None
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [] if IS_PYTHON_3_3 else [
            dlint.linters.base.Flake8Result(
                lineno=6,
                col_offset=4,
                message=dlint.linters.YieldReturnStatementLinter._error_tmpl
            )
        ]

        assert result == expected 
Example #2
Source File: core.py    From dtella with GNU General Public License v2.0 6 votes vote down vote up
def start(self):
        CHECK(self.deferred is None)
        self.deferred = defer.Deferred()

        self.main.showLoginStatus("Scanning For Online Nodes...", counter=1)

        # Get the main UDP socket's bind interface (usually empty)
        bind_ip = self.main.ph.transport.interface

        # Listen on an arbitrary UDP port
        try:
            reactor.listenUDP(0, self, interface=bind_ip)
        except twisted.internet.error.BindError:
            self.main.showLoginStatus("Failed to bind alt UDP port!")
            self.deferred.callback(('no_nodes', None))
        else:
            self.scheduleInitRequest()

        return self.deferred 
Example #3
Source File: test_fs.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_wait_raises_exception_when_time_has_run_out(self):
        clock = self.patch(internet, "reactor", Clock())
        sleep = self.patch(fs_module, "sleep")
        sleep.side_effect = clock.advance

        lock = self.make_lock()
        do_lock = self.patch(lock._fslock, "lock")
        do_unlock = self.patch(lock._fslock, "unlock")

        do_lock.return_value = False

        with ExpectedException(self.locktype.NotAvailable):
            with lock.wait(0.2):
                pass

        self.assertThat(do_lock, MockCallsMatch(call(), call(), call()))
        self.assertThat(sleep, MockCallsMatch(call(0.1), call(0.1)))
        self.assertThat(do_unlock, MockNotCalled()) 
Example #4
Source File: test_fs.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_wait_waits_until_lock_can_be_acquired(self):
        clock = self.patch(internet, "reactor", Clock())
        sleep = self.patch(fs_module, "sleep")
        sleep.side_effect = clock.advance

        lock = self.make_lock()
        do_lock = self.patch(lock._fslock, "lock")
        do_unlock = self.patch(lock._fslock, "unlock")

        do_lock.side_effect = [False, False, True]

        with lock.wait(10):
            self.assertThat(do_lock, MockCallsMatch(call(), call(), call()))
            self.assertThat(sleep, MockCallsMatch(call(1.0), call(1.0)))
            self.assertThat(do_unlock, MockNotCalled())

        self.assertThat(do_unlock, MockCalledOnceWith()) 
Example #5
Source File: test_twisted.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_returns_Deferred_that_wont_be_cancelled_if_errored(self):
        clock = self.patch(internet, "reactor", Clock())

        # Called without a function argument, `deferWithTimeout` returns a new
        # Deferred, and schedules it to be cancelled in `timeout` seconds.
        timeout = randint(10, 100)
        d = deferWithTimeout(timeout)
        [delayed_call] = clock.getDelayedCalls()

        # Advance some amount of time to simulate something happening, but
        # less than the timeout.
        clock.advance(timeout - 1)
        # The timeout call is still in place.
        self.assertThat(delayed_call, DelayedCallActive)

        error = RuntimeError()
        d.errback(error)
        # After calling d the timeout call has been cancelled.
        self.assertThat(delayed_call, DelayedCallCancelled)
        # The error has been passed safely on.
        self.assertRaises(RuntimeError, extract_result, d) 
Example #6
Source File: test_twisted.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_returns_Deferred_that_wont_be_cancelled_if_called(self):
        clock = self.patch(internet, "reactor", Clock())

        # Called without a function argument, `deferWithTimeout` returns a new
        # Deferred, and schedules it to be cancelled in `timeout` seconds.
        timeout = randint(10, 100)
        d = deferWithTimeout(timeout)
        [delayed_call] = clock.getDelayedCalls()

        # Advance some amount of time to simulate something happening.
        clock.advance(5)
        # The timeout call is still in place.
        self.assertThat(delayed_call, DelayedCallActive)

        d.callback(sentinel.result)
        # After calling d the timeout call has been cancelled.
        self.assertThat(delayed_call, DelayedCallCancelled)
        # The result has been safely passed on.
        self.assertThat(extract_result(d), Is(sentinel.result)) 
Example #7
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_long_import(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet.defer import inlineCallbacks

            @inlineCallbacks
            def func(arg):
                return arg
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [] if IS_PYTHON_3_3 else [
            dlint.linters.base.Flake8Result(
                lineno=6,
                col_offset=4,
                message=dlint.linters.YieldReturnStatementLinter._error_tmpl
            )
        ]

        assert result == expected 
Example #8
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_short_import(self):
        python_node = self.get_ast_node(
            """
            import twisted

            @twisted.internet.defer.inlineCallbacks
            def func(arg):
                return arg
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [] if IS_PYTHON_3_3 else [
            dlint.linters.base.Flake8Result(
                lineno=6,
                col_offset=4,
                message=dlint.linters.YieldReturnStatementLinter._error_tmpl
            )
        ]

        assert result == expected 
Example #9
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_nested_class_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if arg:
                    arg += 5

                class inner_class:
                    def inner_func():
                        return arg

                return
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #10
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_nested_function_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if arg:
                    arg += 5

                def inner_func():
                    return arg

                return
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #11
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_non_empty_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                return arg
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [] if IS_PYTHON_3_3 else [
            dlint.linters.base.Flake8Result(
                lineno=6,
                col_offset=4,
                message=dlint.linters.YieldReturnStatementLinter._error_tmpl
            )
        ]

        assert result == expected 
Example #12
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_empty_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                return
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = []

        assert result == expected 
Example #13
Source File: support.py    From Tickeys-linux with MIT License 6 votes vote down vote up
def uninstall_twisted_reactor():
    '''Uninstalls the Kivy's threaded Twisted Reactor. No more Twisted
    tasks will run after this got called. Use this to clean the
    `twisted.internet.reactor` .

    .. versionadded:: 1.9.0
    '''

    import twisted

    # prevent uninstalling more than once
    if not hasattr(twisted, '_kivy_twisted_reactor_installed'):
        return

    from kivy.base import EventLoop

    global _twisted_reactor_stopper
    _twisted_reactor_stopper()
    EventLoop.unbind(on_stop=_twisted_reactor_stopper)

    del twisted._kivy_twisted_reactor_installed 
Example #14
Source File: support.py    From Tickeys-linux with MIT License 6 votes vote down vote up
def uninstall_twisted_reactor():
    '''Uninstalls the Kivy's threaded Twisted Reactor. No more Twisted
    tasks will run after this got called. Use this to clean the
    `twisted.internet.reactor` .

    .. versionadded:: 1.9.0
    '''

    import twisted

    # prevent uninstalling more than once
    if not hasattr(twisted, '_kivy_twisted_reactor_installed'):
        return

    from kivy.base import EventLoop

    global _twisted_reactor_stopper
    _twisted_reactor_stopper()
    EventLoop.unbind(on_stop=_twisted_reactor_stopper)

    del twisted._kivy_twisted_reactor_installed 
Example #15
Source File: test_process.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_deprecated(self):
        """
        Accessing L{PotentialZombieWarning} via the
        I{PotentialZombieWarning} attribute of L{twisted.internet.error}
        results in a deprecation warning being emitted.
        """
        from twisted.internet import error
        error.PotentialZombieWarning

        warnings = self.flushWarnings([self.test_deprecated])
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "twisted.internet.error.PotentialZombieWarning was deprecated in "
            "Twisted 10.0.0: There is no longer any potential for zombie "
            "process.")
        self.assertEqual(len(warnings), 1) 
Example #16
Source File: test_process.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_deprecated(self):
        """
        Accessing L{PotentialZombieWarning} via the
        I{PotentialZombieWarning} attribute of L{twisted.internet.error}
        results in a deprecation warning being emitted.
        """
        from twisted.internet import error
        error.PotentialZombieWarning

        warnings = self.flushWarnings([self.test_deprecated])
        self.assertEqual(warnings[0]['category'], DeprecationWarning)
        self.assertEqual(
            warnings[0]['message'],
            "twisted.internet.error.PotentialZombieWarning was deprecated in "
            "Twisted 10.0.0: There is no longer any potential for zombie "
            "process.")
        self.assertEqual(len(warnings), 1) 
Example #17
Source File: test_twisted.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_set_notifies_all_waiters_that_have_not_timed_out(self):
        clock = self.patch(internet, "reactor", Clock())
        dvalue = DeferredValue()
        waiter0 = dvalue.get()
        waiter1 = dvalue.get(1)
        waiter2 = dvalue.get(3)
        clock.advance(2)
        dvalue.set(sentinel.value)
        self.expectThat(extract_result(waiter0), Is(sentinel.value))
        self.expectThat(extract_result(waiter2), Is(sentinel.value))
        self.assertRaises(CancelledError, extract_result, waiter1) 
Example #18
Source File: test_twisted.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_returns_a_Deferred_with_a_timeout(self):
        clock = self.patch(internet, "reactor", Clock())
        dvalue = DeferredValue()
        waiter = dvalue.get(10)
        self.assertThat(waiter, IsUnfiredDeferred())
        clock.advance(9)
        self.assertThat(waiter, IsUnfiredDeferred())
        clock.advance(1)
        self.assertRaises(CancelledError, extract_result, waiter) 
Example #19
Source File: test_twisted.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_calls_given_function(self):
        clock = self.patch(internet, "reactor", Clock())

        class OurDeferred(Deferred):
            """A Deferred subclass that we use as a marker."""

        # Any given function is called via `maybeDeferred`. In this case, we
        # get an instance of our marker class back because it is a Deferred.
        timeout = randint(10, 100)
        d = deferWithTimeout(timeout, OurDeferred)
        self.assertThat(d, IsInstance(OurDeferred))
        self.assertFalse(d.called)

        # Just as with the non-function form, it's been scheduled for
        # cancellation in `timeout` seconds.
        self.assertThat(clock.getDelayedCalls(), HasLength(1))
        [delayed_call] = clock.getDelayedCalls()
        self.assertThat(delayed_call, DelayedCallActive)
        self.assertThat(
            delayed_call,
            MatchesStructure.byEquality(
                time=timeout, func=d.cancel, args=(), kw={}
            ),
        )

        # Once the timeout is reached, the delayed call is called, and this
        # cancels `d`. The default canceller for Deferred errbacks with
        # CancelledError.
        clock.advance(timeout)
        self.assertThat(delayed_call, DelayedCallCalled)
        self.assertRaises(CancelledError, extract_result, d) 
Example #20
Source File: test_process.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_unimportableOnWindows(self):
        """
        L{twisted.internet.process} is unimportable on Windows.
        """
        with self.assertRaises(ImportError):
            import twisted.internet.process
            twisted.internet.process # shh pyflakes 
Example #21
Source File: test_twisted.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_returns_Deferred_that_will_be_cancelled_after_timeout(self):
        clock = self.patch(internet, "reactor", Clock())

        # Called with only a timeout, `deferWithTimeout` returns a Deferred.
        timeout = randint(10, 100)
        d = deferWithTimeout(timeout)
        self.assertThat(d, IsInstance(Deferred))
        self.assertFalse(d.called)

        # It's been scheduled for cancellation in `timeout` seconds.
        self.assertThat(clock.getDelayedCalls(), HasLength(1))
        [delayed_call] = clock.getDelayedCalls()
        self.assertThat(delayed_call, DelayedCallActive)
        self.assertThat(
            delayed_call,
            MatchesStructure.byEquality(
                time=timeout, func=d.cancel, args=(), kw={}
            ),
        )

        # Once the timeout is reached, the delayed call is called, and this
        # cancels `d`. The default canceller for Deferred errbacks with
        # CancelledError.
        clock.advance(timeout)
        self.assertThat(delayed_call, DelayedCallCalled)
        self.assertRaises(CancelledError, extract_result, d) 
Example #22
Source File: test_send_beacons.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super().setUp()
        self.write_secret()
        self.protocol_mock = self.patch(
            send_beacons_module, "BeaconingSocketProtocol"
        )
        self.fake_protocol = FakeBeaconingSocketProtocol()
        self.protocol_mock.return_value = self.fake_protocol
        self.fake_protocol.send_multicast_beacons = Mock()
        self.fake_protocol.send_beacon = Mock()
        # Prevent the command from starting and stopping the reactor.
        self.reactor_mock = self.patch(internet, "reactor", Clock())
        self.patch(self.reactor_mock, "run") 
Example #23
Source File: test_process.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_processCommandLineArguments(self):
        """
        Arguments given to spawnProcess are passed to the child process as
        originally intended.
        """
        us = b"twisted.internet.test.process_cli"

        args = [b'hello', b'"', b' \t|<>^&', br'"\\"hello\\"', br'"foo\ bar baz\""']
        # Ensure that all non-NUL characters can be passed too.
        allChars = "".join(map(chr, range(1, 255)))
        if isinstance(allChars, unicode):
            allChars.encode("utf-8")

        reactor = self.buildReactor()

        def processFinished(finishedArgs):
            output, err, code = finishedArgs
            output = output.split(b'\0')
            # Drop the trailing \0.
            output.pop()
            self.assertEqual(args, output)

        def shutdown(result):
            reactor.stop()
            return result

        def spawnChild():
            d = succeed(None)
            d.addCallback(lambda dummy: utils.getProcessOutputAndValue(
                pyExe, [b"-m", us] + args, env=properEnv,
                reactor=reactor))
            d.addCallback(processFinished)
            d.addBoth(shutdown)

        reactor.callWhenRunning(spawnChild)
        self.runReactor(reactor) 
Example #24
Source File: test_process.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_unimportableOnWindows(self):
        """
        L{twisted.internet.process} is unimportable on Windows.
        """
        with self.assertRaises(ImportError):
            import twisted.internet.process
            twisted.internet.process # shh pyflakes 
Example #25
Source File: test_tls.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def buildReactor(self):
        reactor = ReactorBuilder.buildReactor(self)
        from twisted.internet import _producer_helpers

        # Patch twisted.protocols.tls to use this reactor, until we get
        # around to fixing #5206, or the TLS code uses an explicit reactor:
        cooperator = Cooperator(
            scheduler=lambda x: reactor.callLater(0.00001, x))
        self.patch(_producer_helpers, "cooperate", cooperator.cooperate)
        return reactor 
Example #26
Source File: test_yield_return_statement.py    From dlint with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_multiple_return_statement(self):
        python_node = self.get_ast_node(
            """
            from twisted.internet import defer

            @defer.inlineCallbacks
            def func(arg):
                if condition:
                    return

                if arg > 10:
                    return
                else:
                    return arg
            """
        )

        linter = dlint.linters.YieldReturnStatementLinter()
        linter.visit(python_node)

        result = linter.get_results()
        expected = [] if IS_PYTHON_3_3 else [
            dlint.linters.base.Flake8Result(
                lineno=12,
                col_offset=8,
                message=dlint.linters.YieldReturnStatementLinter._error_tmpl
            )
        ]

        assert result == expected 
Example #27
Source File: test_twistd.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_applicationRunnerChoosesReactorIfNone(self):
        """
        L{ApplicationRunner} chooses a reactor if none is specified.
        """
        reactor = DummyReactor()
        self.patch(internet, 'reactor', reactor)
        runner = app.ApplicationRunner({
            "profile": False,
            "profiler": "profile",
            "debug": False})
        runner.startReactor(None, None, None)
        self.assertTrue(reactor.called) 
Example #28
Source File: ssh.py    From opencanary with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def getService(self):
        factory = HoneyPotSSHFactory(version=self.version, logger=self.logger)
        factory.canaryservice = self
        factory.portal = portal.Portal(HoneyPotRealm())

        rsa_pubKeyString, rsa_privKeyString = getRSAKeys()
        dsa_pubKeyString, dsa_privKeyString = getDSAKeys()
        factory.portal.registerChecker(HoneypotPasswordChecker(logger=factory.logger))
        factory.portal.registerChecker(CanaryPublicKeyChecker(logger=factory.logger))
        factory.publicKeys = {b'ssh-rsa': keys.Key.fromString(data=rsa_pubKeyString),
                              b'ssh-dss': keys.Key.fromString(data=dsa_pubKeyString)}
        factory.privateKeys = {b'ssh-rsa': keys.Key.fromString(data=rsa_privKeyString),
                               b'ssh-dss': keys.Key.fromString(data=dsa_privKeyString)}
        return internet.TCPServer(self.port, factory, interface=self.listen_addr) 
Example #29
Source File: test_process.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_processCommandLineArguments(self):
        """
        Arguments given to spawnProcess are passed to the child process as
        originally intended.
        """
        us = b"twisted.internet.test.process_cli"

        args = [b'hello', b'"', b' \t|<>^&', br'"\\"hello\\"', br'"foo\ bar baz\""']
        # Ensure that all non-NUL characters can be passed too.
        if _PY3:
            args.append("".join(map(chr, xrange(1,255))).encode("utf8"))
        else:
            args.append("".join(map(chr, xrange(1,255))))

        reactor = self.buildReactor()

        def processFinished(finishedArgs):
            output, err, code = finishedArgs
            output = output.split(b'\0')
            # Drop the trailing \0.
            output.pop()
            self.assertEqual(args, output)

        def shutdown(result):
            reactor.stop()
            return result

        def spawnChild():
            d = succeed(None)
            d.addCallback(lambda dummy: utils.getProcessOutputAndValue(
                pyExe, [b"-m", us] + args, env=properEnv,
                reactor=reactor))
            d.addCallback(processFinished)
            d.addBoth(shutdown)

        reactor.callWhenRunning(spawnChild)
        self.runReactor(reactor) 
Example #30
Source File: tabulate_model_performance.py    From neural_wfst with MIT License 5 votes vote down vote up
def server_method(args):
    import spyne, twisted
    from twisted.internet import reactor
    from twisted import web
    import urllib
    from spyne.protocol import http
    from spyne.protocol import csv
    from spyne.server import twisted

    class HelloWorldService(spyne.service.ServiceBase):
        @spyne.decorator.srpc(
            spyne.model.primitive.Unicode,
            _returns=spyne.model.complex.Iterable(
                spyne.model.primitive.Unicode))
        def my_method(path):
            path = urllib.unquote(path)
            model_run_to_total_epochs_map = map_task_to_epochs()
            yield the_printing_method(path, args, model_run_to_total_epochs_map)[1]

    application = spyne.application.Application(
        [HelloWorldService],
        tns='spyne.examples.hello',
        in_protocol=spyne.protocol.http.HttpRpc(validator='soft'),
        out_protocol=spyne.protocol.csv.Csv())

    reactor.listenTCP(
        8729,
        web.server.Site(
            spyne.server.twisted.TwistedWebResource(
                application)),
        interface='0.0.0.0')

    #------------------------------#
    # Where it all comes together. #
    #------------------------------#
    reactor.run()
    return