我想有兩個客戶端,並在RPyC一個服務器之間的連接,我想從CLIENT1在服務器調用客戶端2的方法的方法調用服務器的方法,這是我的代碼:如何獲取連接到RPyC服務器的客戶端列表?
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個模塊的代碼...... 請爲我解釋一遍 和給我建議任何client.getting ID的ID和其他的東西 你知道,我有一個服務器和兩個客戶端,一個客戶端運行一個簡單的PyQt程序,得到一個瑪雅python命令,並單擊其按鈕,我想運行命令客戶端2上運行的瑪雅,確定?但是,我想將兩個客戶端連接起來,並將它們的方法作爲對等連接進行調用。但我不知道該怎麼稱呼從客戶端1(PyQt的)客戶端2(瑪雅)的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)
我認爲最後一段可能是最相關的。看起來好像OP已經獲得了讓一個客戶端與服務器交談的基本概念。但是沒有廣播消息的概念,也沒有將它們從一個特定的客戶端路由到另一個特定的客戶端。 – jdi