我想在 RPyC 中的两个客户端和一个服务器之间建立连接,并且我想从 client1 调用服务器的方法,在服务器的方法中调用客户端 2 的方法,这是我的代码:
import rpyc
#server:
class ServerService(rpyc.Service):
def on_connect(self):
print "Connected To Server\n"
def on_disconnect(self):
print "Disconnected From Server\n"
def exposed_command(self, cmd):
self._cmd = cmd
self._conn.root.run_command(self._cmd)
#client1:
class AppService(rpyc.Service):
def exposed_foo():
return "foo"
conn = rpyc.connect("localhost", 2014, service = AppService)
conn.root.command(self._cmd)
#client2:
class ClientService(rpyc.Service):
def exposed_run_command(self, cmd):
eval(cmd)
# connect to server
conn = rpyc.connect("localhost", 2014, service = ClientService)
我有 3 个单独的文件,我想通过服务器连接 2 个客户端。
更新
我尝试更多地解释我的情况并添加我使用的 3 个模块的代码......请为我解释更多并给我关于任何客户端 ID 的建议。获取 ID 和其他你知道的东西,我有一个服务器和两个客户端,一个客户端运行一个简单的 PyQt 程序,该程序获取一个 maya python 命令,并单击它的按钮,我想在客户端 2 上运行在 Maya 上运行的命令,好吗?但是,我想将两个客户端连接在一起,并将它们的方法作为对等连接相互调用。但我不知道如何从 client1(PyQt) 调用客户端 2(maya) 的 run_command
服务器端:
import rpyc
class ServerService(rpyc.Service):
def on_connect(self):
print "Connected To Server\n"
def on_disconnect(self):
print "Disconnected From Server\n"
def exposed_command(self, cmd):
self._cmd = cmd
self._conn.root.run_command(self._cmd)
if __name__ == "__main__":
from rpyc.utils.server import ThreadedServer
t = ThreadedServer(ServerService, port = 2014)
print "Server is ready ..."
t.start()
客户端1(PyQt):
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import rpyc
class ClientApp(QDialog):
def __init__(self, parent = None):
super(ClientApp, self).__init__(parent)
self.label = QLabel("CMD: ")
self.textBox = QLineEdit()
self.button = QPushButton("Run Command")
layout = QHBoxLayout(self)
layout.addWidget(self.label)
layout.addWidget(self.textBox)
layout.addWidget(self.button)
self.setWindowTitle("Client App")
# SIGNALS
self.button.clicked.connect(self.sendCommand)
self._cmd = str(self.textBox.text())
self.sendCommand()
def sendCommand(self):
print "Printed Command : "
self._cmd = str(self.textBox.text())
conn.root.command(self._cmd)
class AppService(rpyc.Service):
def exposed_foo2():
return "foo2"
conn = rpyc.connect("localhost", 2014, service = AppService)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ClientApp()
window.show()
客户 2(玛雅):
import rpyc
from maya import cmds
class ClientService(rpyc.Service):
def exposed_run_command(self, cmd):
eval(cmd)
# connect to server
conn = rpyc.connect("localhost", 2014, service = ClientService)