在RPyC 3中重定向标准输入/输出

0 投票
1 回答
619 浏览
提问于 2025-04-18 05:27

正如这里所解释的,使用RPyC 2来重定向标准输入输出非常简单。这意味着在服务器上执行的'print'命令,会在客户端显示出来。

RPyC 2,如这里所说,是不安全的,不推荐使用,但我找不到关于如何在RPyC 3中在客户端打印的任何信息。

有没有人知道怎么做到这一点?

编辑:

例如,这是我服务器的代码:

import rpyc
import time
from rpyc.utils.server import ThreadedServer

class TimeService(rpyc.Service):
    def exposed_print_time(self):
        for i in xrange(10):
            print time.ctime()
            time.sleep(1)

if __name__ == "__main__":
    t = ThreadedServer(TimeService, port=8000)
    t.start()

而在我的客户端,我运行:

conn = rpyc.connect("192.168.1.5")
conn.root.print_time()

我的目标是每秒获取一次时间(或者我想打印的任何内容)并在客户端的标准输出中显示,但客户端只是卡住了,时间只在服务器上打印。

1 个回答

0

你可以在服务器端使用返回值:

#on server
def exposed_test(self):
   return "this is test"

#on client
conn.root.test()
>>> 'this is test'

撰写回答