如何在RPyC中获取连接到服务器的客户端列表?

2024-05-13 02:43:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在RPyC中有两个客户端和一个服务器之间的连接,我想从client1调用一个server方法,在server方法中调用client 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个模块,我使用。。。 请给我解释一下 给我一些建议客户。获取身份证和其他东西 你知道,我有一个服务器和两个客户机,一个客户机运行一个简单的PyQt程序,可以得到一个mayapython命令,然后点击它的按钮,我想在客户端2上运行命令,好吗?但是,我想把这两个客户机连接在一起,并以对等连接的方式互相调用它们的方法。但是我不知道如何从client1(PyQt)调用client2(maya)的run_命令

服务器端:

^{pr2}$

客户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(maya):

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)

Tags: 方法runimportselfcmd客户端serverdef
1条回答
网友
1楼 · 发布于 2024-05-13 02:43:05

以下是一些需要解决的问题:

添加一行来实例化ServerService。假设你已经有了,但是没有显示出来(请更新你的问题),那么下面的内容可能适用。在

conn.root.command(self._cmd)不是对象的一部分,它是client 1脚本的一部分,因此“self”并不表示任何内容。这应该是conn.root.command("run_command")。在

然后在你的ServerService.exposed_命令(self,cmd),应收到cmd=“run_command”。打印出来确认一下。在

然后进来ServerService.exposed_命令(self,命令),你有

self._conn.root.run_command(self._cmd)

我已经有一段时间没有使用rpyc了,所以我不再熟悉它的细节了,但是它怎么知道该向哪个客户端发出命令呢?我猜客户机2要么必须有一个ID,服务器服务应该用给定的ID将命令发送给客户机,要么假设某种广播机制(服务器将接收到的命令发送给所有客户机)。在

相关问题 更多 >