如何将 cmd1 的标准输出通过管道传输到扭曲的 cmd2 的标准输入,其中 cmd1 进程在扭曲的客户端上,而 cmd2 是服务器上的进程?
How do I pipe stdout of cmd1 to stdin of cmd2 in twisted where cmd1 process is on a twisted client and cmd2 is a process on a server?
我如何将一个 spawnProcess 的标准输出连接到另一个 spawnProcess 的标准输入,其中带有 stdout 的 spawnProcess 在客户端上,而 stdin spawnprocess 在服务器上? bash 中的 CLI 命令是 btrfs send @mysubvol | btrfs receive /some/path/
。使用 rfd, wrd = os.pipe()
我设法在服务器端将 process1 传递给 process2。 (现在我想将它从客户端通过管道传输到服务器)。以下代码显示了在同一侧管道传输的进程:
代码如下
from twisted.internet import protocol
from twisted.internet import reactor
import os
class Writer(protocol.ProcessProtocol):
def connectionMade(self):
print "Writer -- connection made"
self.transport.closeChildFD(0)
def childDataReceived(self, fd):
pass
def processEnded(self, status):
pass
class Reader(protocol.ProcessProtocol):
def __init__(self):
pass
def connectionMade(self):
print "Reader -- connection made"
pass
def childDataReceived(self, fd):
print "Reader -- childDataReceived"
def processEnded(self, status):
print "process ended, got:"
def test2():
rfd, wfd = os.pipe()
p1 = reactor.spawnProcess(Writer(), "btrfs", ["btrfs", "send", "/@mySubvol"],env=None, childFDs={0:"w", 1: wfd })
p2 = reactor.spawnProcess(Reader(), "btrfs", ["btrfs", "receive", "/subvolContainer/"], env=None, childFDs={0: rfd, 1: "r"})
os.close(rfd)
os.close(wfd)
reactor.run()
test2()
我试过了:
server.py
from twisted.internet.protocol import Protocol, Factory, ClientFactory
from twisted.internet import protocol
from twisted.internet import reactor
import os
class Reader(protocol.ProcessProtocol):
def __init__(self):
pass
def connectionMade(self):
print "Reader -- connection made"
pass
def childDataReceived(self, fd):
print "Reader -- childDataReceived"
def processEnded(self, status):
print "process ended, got:"
class EchoClientFactory(ClientFactory):
protocol = Reader
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
f = Factory()
reactor.listenTCP(8000, f)
rfd = os.pipe()
p2 = reactor.spawnProcess(Reader(), "btrfs", ["btrfs", "receive", "/"], env=None, childFDs={0: rfd, 1: "r"})
os.close(rfd)
reactor.run()
if __name__ == '__main__':
main()
client.py
from twisted.internet import reactor
from twisted.internet import protocol
import sys
import os
class Writer(protocol.ProcessProtocol):
def connectionMade(self):
print "Writer -- connection made"
self.transport.closeChildFD(0)
def childDataReceived(self, fd):
pass
def processEnded(self, status):
pass
class EchoClientFactory(protocol.ClientFactory):
protocol = Writer
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
factory = EchoClientFactory()
rfd, wfd = os.pipe()
p1 = reactor.spawnProcess(Writer(), "btrfs", ["btrfs", "send", "/home/philipp/testEnv/a2a/@a2"], env=None, childFDs={0:"w", 1: wfd })
reactor.connectTCP('localhost', 8000, factory)
os.close(wfd)
reactor.run()
if __name__ == '__main__':
main()
显然我的尝试是错误的,因为服务器不知道客户端的标准输出管道,但我不知道如何将客户端的 spawnProcess 标准输出通过管道传输到服务器。
更新 01:
根据 Jean-Paul 的回答,我在客户端和服务器上创建了两个协议(ProcessProtocol
和 TCP-Protocol
在每一侧)。我可以成功地将快照从客户端发送到服务器。在客户端上,我必须使用我的 TCP-Protocol
实例启动 ProcessProtocol
,以便它们相互连接。参见:http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother
client.py
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ProcessProtocol
from twisted.internet import reactor
import sys
import os
class Writer(Protocol):
def connectionMade(self): # Called when a connection is made
print "connec made"
proc = MyProcessProtocol(self)
p1 = reactor.spawnProcess(proc, "btrfs", ["btrfs", "send", "/home/user/testEnv/a2a/@a2"])
class EchoClientFactory(ClientFactory):
protocol = Writer
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
class MyProcessProtocol(ProcessProtocol):
def __init__(self, instance):
self.w = instance
def outReceived(self, data): # Some data was received from stdout
self.w.transport.write(data) # Write some data to the physical connection, in sequence, in a non-blocking fashion
def main():
factory = EchoClientFactory()
reactor.connectTCP('localhost', 8000, factory)
reactor.run()
if __name__ == '__main__':
main()
server.py
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ProcessProtocol, ServerFactory
from twisted.internet import reactor
import os
class Reader(Protocol):
def connectionMade(self):
print "connected"
self.r2 = Reader2()
p1 = reactor.spawnProcess(self.r2, "btrfs", ["btrfs", "receive", "/"])
def dataReceived(self, data):
print "dataReceived"
self.r2.transport.write(data)
class Reader2(ProcessProtocol):
def connectionMade(self):
print "connectionMade!"
def processEnded(self, reason):
print "quitting"
def main():
f = ServerFactory()
f.protocol = Reader
reactor.listenTCP(8000, f)
reactor.run()
if __name__ == '__main__':
main()
您不能跨机器设置管道。您也不能在 shell 中执行此操作。 shell 表达式:
btrfs 发送@mysubvol | btrfs 接收 /some/path/
在一台 单台 机器上运行两个 btrfs
进程,并通过管道连接它们。
管道纯粹是本地的。它们不能跨机器共享。为此,您还需要其他东西。例如,TCP 连接。
您已经朝着正确的方向迈出了几步。您的服务器启动 TCP 服务器。您的客户端尝试与该服务器建立新连接。
但是您的服务器没有定义任何处理它接受的连接的行为。并且您的客户端使用 ProcessProtocol
来定义其行为 - 当 TCP 连接不是进程时。
您可能错过的一个想法是,在您的客户端上您将需要两个 协议。一个协议连接到 btrfs send
进程并读取其标准输出。另一个协议连接到您的服务器,可以将 btrfs send
输出写入 TCP 连接。
你的服务器也需要两个协议。其中之一处理来自客户端的连接并读取客户端正在写入该连接的数据。另一个连接到 btrfs receive
进程并将从 TCP 连接读取的数据写入该进程的标准输入。
您发现了 spawnProcess
的 childFDs
功能,这是一个很好的 local 优化。虽然你可以在技术上使用它来帮助你连接两台不同机器上的两个进程,但它涉及你可能不想打扰的额外步骤(至少在你对 "regular" 转发器感到满意之前) .
相反,您只需要一些协议从靠近您的设置发送端的对象接收数据(通过 childDataReceived 和 dataReceived)并将其传递给靠近您设置的接收端的对象(通过 transport.write).
我如何将一个 spawnProcess 的标准输出连接到另一个 spawnProcess 的标准输入,其中带有 stdout 的 spawnProcess 在客户端上,而 stdin spawnprocess 在服务器上? bash 中的 CLI 命令是 btrfs send @mysubvol | btrfs receive /some/path/
。使用 rfd, wrd = os.pipe()
我设法在服务器端将 process1 传递给 process2。 (现在我想将它从客户端通过管道传输到服务器)。以下代码显示了在同一侧管道传输的进程:
代码如下
from twisted.internet import protocol
from twisted.internet import reactor
import os
class Writer(protocol.ProcessProtocol):
def connectionMade(self):
print "Writer -- connection made"
self.transport.closeChildFD(0)
def childDataReceived(self, fd):
pass
def processEnded(self, status):
pass
class Reader(protocol.ProcessProtocol):
def __init__(self):
pass
def connectionMade(self):
print "Reader -- connection made"
pass
def childDataReceived(self, fd):
print "Reader -- childDataReceived"
def processEnded(self, status):
print "process ended, got:"
def test2():
rfd, wfd = os.pipe()
p1 = reactor.spawnProcess(Writer(), "btrfs", ["btrfs", "send", "/@mySubvol"],env=None, childFDs={0:"w", 1: wfd })
p2 = reactor.spawnProcess(Reader(), "btrfs", ["btrfs", "receive", "/subvolContainer/"], env=None, childFDs={0: rfd, 1: "r"})
os.close(rfd)
os.close(wfd)
reactor.run()
test2()
我试过了:
server.py
from twisted.internet.protocol import Protocol, Factory, ClientFactory
from twisted.internet import protocol
from twisted.internet import reactor
import os
class Reader(protocol.ProcessProtocol):
def __init__(self):
pass
def connectionMade(self):
print "Reader -- connection made"
pass
def childDataReceived(self, fd):
print "Reader -- childDataReceived"
def processEnded(self, status):
print "process ended, got:"
class EchoClientFactory(ClientFactory):
protocol = Reader
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
f = Factory()
reactor.listenTCP(8000, f)
rfd = os.pipe()
p2 = reactor.spawnProcess(Reader(), "btrfs", ["btrfs", "receive", "/"], env=None, childFDs={0: rfd, 1: "r"})
os.close(rfd)
reactor.run()
if __name__ == '__main__':
main()
client.py
from twisted.internet import reactor
from twisted.internet import protocol
import sys
import os
class Writer(protocol.ProcessProtocol):
def connectionMade(self):
print "Writer -- connection made"
self.transport.closeChildFD(0)
def childDataReceived(self, fd):
pass
def processEnded(self, status):
pass
class EchoClientFactory(protocol.ClientFactory):
protocol = Writer
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
def main():
factory = EchoClientFactory()
rfd, wfd = os.pipe()
p1 = reactor.spawnProcess(Writer(), "btrfs", ["btrfs", "send", "/home/philipp/testEnv/a2a/@a2"], env=None, childFDs={0:"w", 1: wfd })
reactor.connectTCP('localhost', 8000, factory)
os.close(wfd)
reactor.run()
if __name__ == '__main__':
main()
显然我的尝试是错误的,因为服务器不知道客户端的标准输出管道,但我不知道如何将客户端的 spawnProcess 标准输出通过管道传输到服务器。
更新 01:
根据 Jean-Paul 的回答,我在客户端和服务器上创建了两个协议(ProcessProtocol
和 TCP-Protocol
在每一侧)。我可以成功地将快照从客户端发送到服务器。在客户端上,我必须使用我的 TCP-Protocol
实例启动 ProcessProtocol
,以便它们相互连接。参见:http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputononeconnectionresultinoutputonanother
client.py
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ProcessProtocol
from twisted.internet import reactor
import sys
import os
class Writer(Protocol):
def connectionMade(self): # Called when a connection is made
print "connec made"
proc = MyProcessProtocol(self)
p1 = reactor.spawnProcess(proc, "btrfs", ["btrfs", "send", "/home/user/testEnv/a2a/@a2"])
class EchoClientFactory(ClientFactory):
protocol = Writer
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason.getErrorMessage()
reactor.stop()
def clientConnectionLost(self, connector, reason):
print 'connection lost:', reason.getErrorMessage()
reactor.stop()
class MyProcessProtocol(ProcessProtocol):
def __init__(self, instance):
self.w = instance
def outReceived(self, data): # Some data was received from stdout
self.w.transport.write(data) # Write some data to the physical connection, in sequence, in a non-blocking fashion
def main():
factory = EchoClientFactory()
reactor.connectTCP('localhost', 8000, factory)
reactor.run()
if __name__ == '__main__':
main()
server.py
from twisted.internet.protocol import Protocol, Factory, ClientFactory, ProcessProtocol, ServerFactory
from twisted.internet import reactor
import os
class Reader(Protocol):
def connectionMade(self):
print "connected"
self.r2 = Reader2()
p1 = reactor.spawnProcess(self.r2, "btrfs", ["btrfs", "receive", "/"])
def dataReceived(self, data):
print "dataReceived"
self.r2.transport.write(data)
class Reader2(ProcessProtocol):
def connectionMade(self):
print "connectionMade!"
def processEnded(self, reason):
print "quitting"
def main():
f = ServerFactory()
f.protocol = Reader
reactor.listenTCP(8000, f)
reactor.run()
if __name__ == '__main__':
main()
您不能跨机器设置管道。您也不能在 shell 中执行此操作。 shell 表达式:
btrfs 发送@mysubvol | btrfs 接收 /some/path/
在一台 单台 机器上运行两个 btrfs
进程,并通过管道连接它们。
管道纯粹是本地的。它们不能跨机器共享。为此,您还需要其他东西。例如,TCP 连接。
您已经朝着正确的方向迈出了几步。您的服务器启动 TCP 服务器。您的客户端尝试与该服务器建立新连接。
但是您的服务器没有定义任何处理它接受的连接的行为。并且您的客户端使用 ProcessProtocol
来定义其行为 - 当 TCP 连接不是进程时。
您可能错过的一个想法是,在您的客户端上您将需要两个 协议。一个协议连接到 btrfs send
进程并读取其标准输出。另一个协议连接到您的服务器,可以将 btrfs send
输出写入 TCP 连接。
你的服务器也需要两个协议。其中之一处理来自客户端的连接并读取客户端正在写入该连接的数据。另一个连接到 btrfs receive
进程并将从 TCP 连接读取的数据写入该进程的标准输入。
您发现了 spawnProcess
的 childFDs
功能,这是一个很好的 local 优化。虽然你可以在技术上使用它来帮助你连接两台不同机器上的两个进程,但它涉及你可能不想打扰的额外步骤(至少在你对 "regular" 转发器感到满意之前) .
相反,您只需要一些协议从靠近您的设置发送端的对象接收数据(通过 childDataReceived 和 dataReceived)并将其传递给靠近您设置的接收端的对象(通过 transport.write).