本文整理汇总了Python中twisted.internet.protocol.ProcessProtocol方法的典型用法代码示例。如果您正苦于以下问题:Python protocol.ProcessProtocol方法的具体用法?Python protocol.ProcessProtocol怎么用?Python protocol.ProcessProtocol使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.protocol
的用法示例。
在下文中一共展示了protocol.ProcessProtocol方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_launchWorkerProcesses
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_launchWorkerProcesses(self):
"""
Given a C{spawnProcess} function, C{launchWorkerProcess} launches a
python process with an existing path as its argument.
"""
protocols = [ProcessProtocol() for i in range(4)]
arguments = []
environment = {}
def fakeSpawnProcess(processProtocol, executable, args=(), env={},
path=None, uid=None, gid=None, usePTY=0,
childFDs=None):
arguments.append(executable)
arguments.extend(args)
environment.update(env)
self.runner.launchWorkerProcesses(
fakeSpawnProcess, protocols, ["foo"])
self.assertEqual(arguments[0], arguments[1])
self.assertTrue(os.path.exists(arguments[2]))
self.assertEqual("foo", arguments[3])
self.assertEqual(os.pathsep.join(sys.path),
environment["TRIAL_PYTHONPATH"])
示例2: test_requestShell
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_requestShell(self):
"""
When a client requests a shell, the SSHSession object should get
the shell by getting an ISession adapter for the avatar, then
calling openShell() with a ProcessProtocol to attach.
"""
# gets a shell the first time
ret = self.session.requestReceived(b'shell', b'')
self.assertTrue(ret)
self.assertSessionIsStubSession()
self.assertIsInstance(self.session.client,
session.SSHSessionProcessProtocol)
self.assertIs(self.session.session.shellProtocol, self.session.client)
# doesn't get a shell the second time
self.assertFalse(self.session.requestReceived(b'shell', b''))
self.assertRequestRaisedRuntimeError()
示例3: test_requestExec
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_requestExec(self):
"""
When a client requests a command, the SSHSession object should get
the command by getting an ISession adapter for the avatar, then
calling execCommand with a ProcessProtocol to attach and the
command line.
"""
ret = self.session.requestReceived(b'exec',
common.NS(b'failure'))
self.assertFalse(ret)
self.assertRequestRaisedRuntimeError()
self.assertIsNone(self.session.client)
self.assertTrue(self.session.requestReceived(b'exec',
common.NS(b'success')))
self.assertSessionIsStubSession()
self.assertIsInstance(self.session.client,
session.SSHSessionProcessProtocol)
self.assertIs(self.session.session.execProtocol, self.session.client)
self.assertEqual(self.session.session.execCommandLine,
b'success')
示例4: test_stderr
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_stderr(self):
"""
Bytes written to stderr by the spawned process are passed to the
C{errReceived} callback on the C{ProcessProtocol} passed to
C{spawnProcess}.
"""
value = "42"
p = Accumulator()
d = p.endedDeferred = defer.Deferred()
reactor.spawnProcess(p, pyExe,
[pyExe, b"-c",
networkString("import sys; sys.stderr.write"
"('{0}')".format(value))],
env=None, path="/tmp",
usePTY=self.usePTY)
def processEnded(ign):
self.assertEqual(b"42", p.errF.getvalue())
return d.addCallback(processEnded)
示例5: test_stdout
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_stdout(self):
"""
ProcessProtocol.transport.closeStdout actually closes the pipe.
"""
d = self.doit(1)
def _check(errput):
if _PY3:
if runtime.platform.isWindows():
self.assertIn(b"OSError", errput)
self.assertIn(b"22", errput)
else:
self.assertIn(b'BrokenPipeError', errput)
else:
self.assertIn(b'OSError', errput)
if runtime.platform.getType() != 'win32':
self.assertIn(b'Broken pipe', errput)
d.addCallback(_check)
return d
示例6: fork
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def fork(executable, args=(), env={}, path=None, timeout=3600):
"""fork
Provides a deferred wrapper function with a timeout function
:param executable: Executable
:type executable: str.
:param args: Tupple of arguments
:type args: tupple.
:param env: Environment dictionary
:type env: dict.
:param timeout: Kill the child process if timeout is exceeded
:type timeout: int.
"""
d = defer.Deferred()
p = ProcessProtocol(d, timeout)
reactor.spawnProcess(p, executable, (executable,)+tuple(args), env, path)
return d
示例7: _respond_failure
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def _respond_failure(self, failure, operation_id, reboot):
logging.info("Shutdown request failed.")
failure_report = '\n'.join([
failure.value.data,
"",
"Attempting to force {operation}. Please note that if this "
"succeeds, Landscape will have no way of knowing and will still "
"mark this activity as having failed. It is recommended you check "
"the state of the machine manually to determine whether "
"{operation} succeeded.".format(
operation="reboot" if reboot else "shutdown")
])
deferred = self._respond(FAILED, failure_report, operation_id)
# Add another callback spawning the poweroff or reboot command (which
# seem more reliable in aberrant situations like a post-trusty release
# upgrade where upstart has been replaced with systemd). If this
# succeeds, we won't have any opportunity to report it and if it fails
# we'll already have responded indicating we're attempting to force
# the operation so either way there's no sense capturing output
protocol = ProcessProtocol()
command, args = self._get_command_and_args(protocol, reboot, True)
deferred.addCallback(
lambda _: self._process_factory.spawnProcess(
protocol, command, args=args))
return deferred
示例8: test_requestExec
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_requestExec(self):
"""
When a client requests a command, the SSHSession object should get
the command by getting an ISession adapter for the avatar, then
calling execCommand with a ProcessProtocol to attach and the
command line.
"""
ret = self.session.requestReceived('exec',
common.NS('failure'))
self.assertFalse(ret)
self.assertRequestRaisedRuntimeError()
self.assertIdentical(self.session.client, None)
self.assertTrue(self.session.requestReceived('exec',
common.NS('success')))
self.assertSessionIsStubSession()
self.assertIsInstance(self.session.client,
session.SSHSessionProcessProtocol)
self.assertIdentical(self.session.session.execProtocol,
self.session.client)
self.assertEquals(self.session.session.execCommandLine,
'success')
示例9: test_stderr
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_stderr(self):
"""
Bytes written to stderr by the spawned process are passed to the
C{errReceived} callback on the C{ProcessProtocol} passed to
C{spawnProcess}.
"""
cmd = sys.executable
value = "42"
p = Accumulator()
d = p.endedDeferred = defer.Deferred()
reactor.spawnProcess(p, cmd,
[cmd, "-c",
"import sys; sys.stderr.write('%s')" % (value,)],
env=None, path="/tmp",
usePTY=self.usePTY)
def processEnded(ign):
self.assertEquals(value, p.errF.getvalue())
return d.addCallback(processEnded)
示例10: makeDeferredWithProcessProtocol
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def makeDeferredWithProcessProtocol():
"""Returns a (`Deferred`, `ProcessProtocol`) tuple.
The Deferred's `callback()` will be called (with None) if the
`ProcessProtocol` is called back indicating that no error occurred.
Its `errback()` will be called with the `Failure` reason otherwise.
"""
done = Deferred()
protocol = ProcessProtocol()
# Call the errback if the "failure" object indicates a non-zero exit.
protocol.processEnded = lambda reason: (
done.errback(reason)
if (reason and not reason.check(ProcessDone))
else done.callback(None)
)
return done, protocol
示例11: errReceived
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def errReceived(self, data):
"""Override ProcessProtocol.errReceived."""
LOG.d("errReceived:", data)
self.err_data += data
示例12: outReceived
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def outReceived(self, data):
"""Override ProcessProtocol.outReceived."""
old_lines_count = self.lines_count
self.lines_count += data.count(b'\n')
LOG.d("outReceived, lines_count = %s:" % self.lines_count, data)
self.out_data += data
# If this is the first time lines_max is exceeded, call stop
if self.lines_count >= self.lines_max > old_lines_count:
self.stop("lines_max")
示例13: processExited
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def processExited(self, reason):
"""Override ProcessProtocol.processExited."""
LOG.d("processExited:", reason.value.exitCode)
self.state = "idle"
# Cancel the timeout and stop callbacks
if self.dc_timeout:
self.dc_timeout.cancel()
self.dc_timeout = None
if self.dc_stop:
self.dc_stop.cancel()
self.dc_stop = None
if not self.reason:
self.reason = str(reason.value)
# noinspection PyUnresolvedReferences
reactor.callLater(0, self.call_on_exit)
示例14: test_noCompatibilityLayer
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def test_noCompatibilityLayer(self):
"""
If no compatibility layer is present, imports of gobject and friends
are disallowed.
We do this by running a process where we make sure gi.pygtkcompat
isn't present.
"""
if _PY3:
raise SkipTest("Python3 always has the compatibility layer.")
from twisted.internet import reactor
if not IReactorProcess.providedBy(reactor):
raise SkipTest("No process support available in this reactor.")
result = Deferred()
class Stdout(ProcessProtocol):
data = b""
def errReceived(self, err):
print(err)
def outReceived(self, data):
self.data += data
def processExited(self, reason):
result.callback(self.data)
path = FilePath(__file__).sibling(b"process_gireactornocompat.py").path
pyExe = FilePath(sys.executable)._asBytesPath()
# Pass in a PYTHONPATH that is the test runner's os.path, to make sure
# we're running from a checkout
reactor.spawnProcess(Stdout(), pyExe, [pyExe, path],
env={"PYTHONPATH": ":".join(sys.path)})
result.addCallback(self.assertEqual, b"success")
return result
示例15: __init__
# 需要导入模块: from twisted.internet import protocol [as 别名]
# 或者: from twisted.internet.protocol import ProcessProtocol [as 别名]
def __init__(self, protocol, data):
"""
@type protocol: L{ConchTestForwardingProcess}
@param protocol: The L{ProcessProtocol} which made this connection.
@type data: str
@param data: The data to be sent to the third-party server.
"""
self.protocol = protocol
self.data = data