1.首先要安裝rpyc模塊;python
#pip install rpyc函數
2.服務端code
__author__ = 'Administrator' import rpyc from rpyc import Service from rpyc.utils.server import ThreadedServer import os class Test(Service): def exposed_cmd(self,cmd): #客戶端要調用的函數方法前面要加‘exposed_’,不然調用失敗! return os.system(cmd) sr = ThreadedServer(Test, port=999, auto_register=False) sr.start()
3.客戶端:server
__author__ = 'Administrator' import rpyc conn = rpyc.connect('localhost',999) cr = conn.root.cmd(cmd) conn.close()
--------------ip
__author__ = 'Administrator' import rpyc try: conn = rpyc.connect('localhost',999) except: print('Connetion fail!') while 1: try: cmd = input('請輸入您的指令:') if cmd == 'q' or cmd=='exit': break cr = conn.root.cmd(cmd) if cr == 0: print(cmd,'執行成功!') else: print(cmd,'執行失敗!') except: conn.close()
4.效果圖:input