Python twisted.internet.reactor() Examples
The following are 30
code examples of twisted.internet.reactor().
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.internet
, or try the search function
.
Example #1
Source File: alias.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def spawnProcess(self, proto, program, path): """ Spawn a process. This wraps the L{spawnProcess <twisted.internet.interfaces.IReactorProcess.spawnProcess>} method on L{reactor} so that it can be customized for test purposes. @type proto: L{IProcessProtocol <twisted.internet.interfaces.IProcessProtocol>} provider @param proto: An object which will be notified of all events related to the created process. @type program: L{bytes} @param program: The full path name of the file to execute. @type path: L{list} of L{bytes} @param path: The arguments to pass to the process. The first string should be the executable's name. @rtype: L{IProcessTransport <twisted.internet.interfaces.IProcessTransport>} provider @return: A process transport. """ return self.reactor.spawnProcess(proto, program, path)
Example #2
Source File: proxy.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def process(self): parsed = urllib_parse.urlparse(self.uri) protocol = parsed[0] host = parsed[1].decode('ascii') port = self.ports[protocol] if ':' in host: host, port = host.split(':') port = int(port) rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' class_ = self.protocols[protocol] headers = self.getAllHeaders().copy() if b'host' not in headers: headers[b'host'] = host.encode('ascii') self.content.seek(0, 0) s = self.content.read() clientFactory = class_(self.method, rest, self.clientproto, headers, s, self) self.reactor.connectTCP(host, port, clientFactory)
Example #3
Source File: proxy.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def __init__(self, host, port, path, reactor=reactor): """ @param host: the host of the web server to proxy. @type host: C{str} @param port: the port of the web server to proxy. @type port: C{port} @param path: the base path to fetch data from. Note that you shouldn't put any trailing slashes in it, it will be added automatically in request. For example, if you put B{/foo}, a request on B{/bar} will be proxied to B{/foo/bar}. Any required encoding of special characters (such as " " or "/") should have been done already. @type path: C{str} """ Resource.__init__(self) self.host = host self.port = port self.path = path self.reactor = reactor
Example #4
Source File: http.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def __init__(self, logPath=None, timeout=_REQUEST_TIMEOUT, logFormatter=None, reactor=None): """ @param logFormatter: An object to format requests into log lines for the access log. @type logFormatter: L{IAccessLogFormatter} provider @param reactor: A L{IReactorTime} provider used to compute logging timestamps. """ if not reactor: from twisted.internet import reactor self._reactor = reactor if logPath is not None: logPath = os.path.abspath(logPath) self.logPath = logPath self.timeOut = timeout if logFormatter is None: logFormatter = combinedLogFormatter self._logFormatter = logFormatter # For storing the cached log datetime and the callback to update it self._logDateTime = None self._logDateTimeCall = None
Example #5
Source File: test_options.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def patchInstallReactor(self): """ Patch C{_options.installReactor} so we can capture usage and prevent actual installs. """ self.installedReactors = {} def installReactor(name): if name != "fusion": raise NoSuchReactor() reactor = MemoryReactor() self.installedReactors[name] = reactor return reactor self.patch(_options, "installReactor", installReactor)
Example #6
Source File: proxy.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def render(self, request): """ Render a request by forwarding it to the proxied server. """ # RFC 2616 tells us that we can omit the port if it's the default port, # but we have to provide it otherwise if self.port == 80: host = self.host else: host = self.host + u":" + str(self.port) request.requestHeaders.setRawHeaders(b"host", [host.encode('ascii')]) request.content.seek(0, 0) qs = urllib_parse.urlparse(request.uri)[4] if qs: rest = self.path + b'?' + qs else: rest = self.path clientFactory = self.proxyClientFactoryClass( request.method, rest, request.clientproto, request.getAllHeaders(), request.content.read(), request) self.reactor.connectTCP(self.host, self.port, clientFactory) return NOT_DONE_YET
Example #7
Source File: intutil.py From afkak with Apache License 2.0 | 6 votes |
def retry_while_broker_errors(self, f, *a, **kw): """ Call a function, retrying on retriable broker errors. If calling the function fails with one of these exception types it is called again after a short delay: * `afkak.common.RetriableBrokerResponseError` (or a subclass thereof) * `afkak.common.PartitionUnavailableError` The net effect is to keep trying until topic auto-creation completes. :param f: callable, which may return a `Deferred` :param a: arbitrary positional arguments :param kw: arbitrary keyword arguments """ while True: try: returnValue((yield f(*a, **kw))) break except (RetriableBrokerResponseError, PartitionUnavailableError): yield async_delay(0.1, clock=self.reactor)
Example #8
Source File: client.py From afkak with Apache License 2.0 | 6 votes |
def _get_brokerclient(self, node_id): """ Get a broker client. :param int node_id: Broker node ID :raises KeyError: for an unknown node ID :returns: :class:`_KafkaBrokerClient` """ if self._closing: raise ClientError("Cannot get broker client for node_id={}: {} has been closed".format(node_id, self)) if node_id not in self.clients: broker_metadata = self._brokers[node_id] log.debug("%r: creating client for %s", self, broker_metadata) self.clients[node_id] = _KafkaBrokerClient( self.reactor, self._endpoint_factory, broker_metadata, self.clientId, self._retry_policy, ) return self.clients[node_id]
Example #9
Source File: strports.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def service(description, factory, reactor=None): """ Return the service corresponding to a description. @param description: The description of the listening port, in the syntax described by L{twisted.internet.endpoints.serverFromString}. @type description: C{str} @param factory: The protocol factory which will build protocols for connections to this service. @type factory: L{twisted.internet.interfaces.IProtocolFactory} @rtype: C{twisted.application.service.IService} @return: the service corresponding to a description of a reliable stream server. @see: L{twisted.internet.endpoints.serverFromString} """ if reactor is None: from twisted.internet import reactor svc = StreamServerEndpointService( endpoints.serverFromString(reactor, description), factory) svc._raiseSynchronously = True return svc
Example #10
Source File: service.py From bitmask-dev with GNU General Public License v3.0 | 6 votes |
def _start_onion_service(self, factory): def progress(percent, tag, message): bar = int(percent / 10) log.debug('[%s%s] %s' % ('#' * bar, '.' * (10 - bar), message)) def setup_complete(port): port = txtorcon.IHiddenService(port) self.uri = "http://%s" % (port.getHost().onion_uri) log.info('I have set up a hidden service, advertised at: %s' % self.uri) log.info('locally listening on %s' % port.local_address.getHost()) def setup_failed(args): log.error('onion service setup FAILED: %r' % args) endpoint = endpoints.serverFromString(reactor, 'onion:80') txtorcon.IProgressProvider(endpoint).add_progress_listener(progress) d = endpoint.listen(factory) d.addCallback(setup_complete) d.addErrback(setup_failed) return d
Example #11
Source File: strports.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def listen(description, factory): """ Listen on a port corresponding to a description. @param description: The description of the connecting port, in the syntax described by L{twisted.internet.endpoints.serverFromString}. @type description: L{str} @param factory: The protocol factory which will build protocols on connection. @type factory: L{twisted.internet.interfaces.IProtocolFactory} @rtype: L{twisted.internet.interfaces.IListeningPort} @return: the port corresponding to a description of a reliable virtual circuit server. @see: L{twisted.internet.endpoints.serverFromString} """ from twisted.internet import reactor name, args, kw = endpoints._parseServer(description, factory) return getattr(reactor, 'listen' + name)(*args, **kw)
Example #12
Source File: abstract.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def connectionLost(self, reason): """ The connection was lost. This is called when the connection on a selectable object has been lost. It will be called whether the connection was closed explicitly, an exception occurred in an event handler, or the other end of the connection closed it first. Clean up state here, but make sure to call back up to FileDescriptor. """ self.disconnected = True self.connected = False if self.producer is not None: self.producer.stopProducing() self.producer = None self.stopReading() self.stopWriting() self.reactor.removeActiveHandle(self)
Example #13
Source File: abstract.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _handleRead(self, rc, data, evt): """ Returns False if we should stop reading for now """ if self.disconnected: return False # graceful disconnection if (not (rc or data)) or rc in (errno.WSAEDISCON, ERROR_HANDLE_EOF): self.reactor.removeActiveHandle(self) self.readConnectionLost(failure.Failure(main.CONNECTION_DONE)) return False # XXX: not handling WSAEWOULDBLOCK # ("too many outstanding overlapped I/O requests") elif rc: self.connectionLost(failure.Failure( error.ConnectionLost("read error -- %s (%s)" % (errno.errorcode.get(rc, 'unknown'), rc)))) return False else: assert self._readSize == 0 assert self._readNextBuffer == 0 self._readSize = data return self._dispatchData()
Example #14
Source File: twisted.py From python-consul2 with MIT License | 6 votes |
def request(self, callback, method, url, **kwargs): if 'data' in kwargs and not isinstance(kwargs['data'], bytes): # python2/3 compatibility data = kwargs.pop('data') kwargs['data'] = data.encode(encoding='utf-8') \ if hasattr(data, 'encode') else b(data) try: response = yield self.client.request(method, url, **kwargs) parsed = yield self._get_resp(response) returnValue(callback(self.response(*parsed))) except ConnectError as e: raise ConsulException( '{}: {}'.format(e.__class__.__name__, e.message)) except ResponseNeverReceived: # this exception is raised if the connection to the server is lost # when yielding a response, this could be due to network issues or # server restarts raise ConsulException( 'Server connection lost: {} {}'.format(method.upper(), url)) except RequestTransmissionFailed: # this exception is expected if the reactor is stopped mid request raise ConsulException( 'Request incomplete: {} {}'.format(method.upper(), url))
Example #15
Source File: alias.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def __init__(self, protocol, process=None, reactor=None): """ @type protocol: L{ProcessAliasProtocol} @param protocol: The protocol associated with the child process. @type process: L{bytes} or L{None} @param process: The process name. @type reactor: L{None} or L{IReactorTime <twisted.internet.interfaces.IReactorTime>} provider @param reactor: A reactor which will be used to schedule timeouts. """ self.processName = process self.protocol = protocol self.completion = defer.Deferred() self.protocol.onEnd = self.completion self.completion.addBoth(self._processEnded) if reactor is not None: self.reactor = reactor
Example #16
Source File: db.py From worker with GNU General Public License v3.0 | 6 votes |
def acquireTriggerCheckLock(self, trigger_id, timeout): """ acquireTriggerCheckLock(self, trigger_id, timeout) Try to acquire lock for trigger check until timeout :param trigger_id: trigger identity :type trigger_id: string :param timeout: timeout in seconds :type timeout: float """ acquired = yield self.setTriggerCheckLock(trigger_id) count = 0 while acquired is None and count < timeout: count += 1 yield task.deferLater(reactor, 0.5, lambda: None) acquired = yield self.setTriggerCheckLock(trigger_id) if acquired is None: raise Exception("Can not acquire trigger lock in {0} seconds".format(timeout))
Example #17
Source File: test_log.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_inCallback(self): """ Log an error in an asynchronous callback. """ return task.deferLater(reactor, 0, lambda: log.err(makeFailure()))
Example #18
Source File: defer.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def __init__(self, name, scheduler=None): """ @param name: The name of the lock to acquire @param scheduler: An object which provides L{IReactorTime} """ lockfile.FilesystemLock.__init__(self, name) if scheduler is None: from twisted.internet import reactor scheduler = reactor self._scheduler = scheduler
Example #19
Source File: test_reporter.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_hiddenException(self): """ Check that errors in C{DelayedCall}s get reported, even if the test already has a failure. Only really necessary for testing the deprecated style of tests that use iterate() directly. See L{erroneous.DelayedCall.testHiddenException} for more details. """ from twisted.internet import reactor if reflect.qual(reactor).startswith("twisted.internet.asyncioreactor"): raise self.skipTest( ("This test does not work on the asyncio reactor, as the " "traceback comes from inside asyncio, not Twisted.")) test = erroneous.DelayedCall('testHiddenException') output = self.getOutput(test).splitlines() errorQual = qual(RuntimeError) match = [ self.doubleSeparator, '[FAIL]', 'Traceback (most recent call last):', re.compile(r'^\s+File .*erroneous\.py., line \d+, in ' 'testHiddenException$'), re.compile(r'^\s+self\.fail\("Deliberate failure to mask the ' 'hidden exception"\)$'), 'twisted.trial.unittest.FailTest: ' 'Deliberate failure to mask the hidden exception', 'twisted.trial.test.erroneous.DelayedCall.testHiddenException', self.doubleSeparator, '[ERROR]', 'Traceback (most recent call last):', re.compile(r'^\s+File .* in runUntilCurrent'), re.compile(r'^\s+.*'), re.compile('^\s+File .*erroneous\.py", line \d+, in go'), re.compile('^\s+raise RuntimeError\(self.hiddenExceptionMsg\)'), errorQual + ': something blew up', 'twisted.trial.test.erroneous.DelayedCall.testHiddenException'] self.stringComparison(match, output)
Example #20
Source File: abstract.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def startWriting(self): """Start waiting for write availability. Call this to have this FileDescriptor be notified whenever it is ready for writing. """ self.reactor.addWriter(self) # Producer/consumer implementation # first, the consumer stuff. This requires no additional work, as # any object you can write to can be a consumer, really.
Example #21
Source File: test_default.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_windows(self): """ L{_getInstallFunction} chooses the select reactor on Windows. """ install = _getInstallFunction(windows) self.assertEqual( install.__module__, 'twisted.internet.selectreactor')
Example #22
Source File: abstract.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def __init__(self, reactor=None): """ @param reactor: An L{IReactorFDSet} provider which this descriptor will use to get readable and writeable event notifications. If no value is given, the global reactor will be used. """ if not reactor: from twisted.internet import reactor self.reactor = reactor self._tempDataBuffer = [] # will be added to dataBuffer in doWrite self._tempDataLen = 0
Example #23
Source File: test_default.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_osx(self): """ L{_getInstallFunction} chooses the select reactor on OS X. """ install = _getInstallFunction(osx) self.assertEqual( install.__module__, 'twisted.internet.selectreactor')
Example #24
Source File: test_default.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_linux(self): """ L{_getInstallFunction} chooses the epoll reactor on Linux, or poll if epoll is unavailable. """ install = _getInstallFunction(linux) if requireModule('twisted.internet.epollreactor') is None: self.assertIsPoll(install) else: self.assertEqual( install.__module__, 'twisted.internet.epollreactor')
Example #25
Source File: test_default.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def test_unix(self): """ L{_getInstallFunction} chooses the poll reactor on arbitrary Unix platforms, falling back to select(2) if it is unavailable. """ install = _getInstallFunction(unix) self.assertIsPoll(install)
Example #26
Source File: abstract.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def startReading(self): self.reactor.addActiveHandle(self) if not self._readScheduled and not self.reading: self.reading = True self._readScheduled = self.reactor.callLater(0, self._resumeReading)
Example #27
Source File: test_default.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def assertIsPoll(self, install): """ Assert the given function will install the poll() reactor, or select() if poll() is unavailable. """ if hasattr(select, "poll"): self.assertEqual( install.__module__, 'twisted.internet.pollreactor') else: self.assertEqual( install.__module__, 'twisted.internet.selectreactor')
Example #28
Source File: _posixstdio.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def __init__(self, proto, stdin=0, stdout=1, reactor=None): if reactor is None: from twisted.internet import reactor self.protocol = proto self._writer = process.ProcessWriter(reactor, self, 'write', stdout) self._reader = process.ProcessReader(reactor, self, 'read', stdin) self._reader.startReading() self.protocol.makeConnection(self) # ITransport # XXX Actually, see #3597.
Example #29
Source File: _win32stdio.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def __init__(self, proto, reactor=None): """ Start talking to standard IO with the given protocol. Also, put it stdin/stdout/stderr into binary mode. """ if reactor is None: from twisted.internet import reactor for stdfd in range(0, 1, 2): msvcrt.setmode(stdfd, os.O_BINARY) _pollingfile._PollingTimer.__init__(self, reactor) self.proto = proto hstdin = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE) hstdout = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE) self.stdin = _pollingfile._PollableReadPipe( hstdin, self.dataReceived, self.readConnectionLost) self.stdout = _pollingfile._PollableWritePipe( hstdout, self.writeConnectionLost) self._addPollableResource(self.stdin) self._addPollableResource(self.stdout) self.proto.makeConnection(self)
Example #30
Source File: abstract.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
def startWriting(self): self.reactor.addActiveHandle(self) self.writing = True if not self._writeScheduled: self._writeScheduled = self.reactor.callLater(0, self._resumeWriting)