python - 在不使用经典 RPyC 的情况下将 RPyC 的标准输出重定向到本地客户端

标签 python stdout io-redirection rpyc

在使用 RPyC 服务时如何将标准输出重定向到我的本地客户端?我知道通过使用

使用 Classic RPyC 是可能的
c = rpyc.classic.connect(ip)

c.modules.sys.stdout = sys.stdout

有没有人有我可以在我的客户端使用的等效代码,用于如下所示的用户定义服务

class remoteServer(rpyc.Service):

    def on_connect(self):
        # code that runs when a connection is created
        # (to init the serivce, if needed)

        if not os.path.exists(self.LOG_DIRECTORY_PATH):
            os.mkdir(self.LOG_DIRECTORY_PATH);
            file = open(self.LOG_FILENAME, 'a');
        else:
            print 'Directory: ', self.LOG_DIRECTORY_PATH, 'already exists';

if __name__ == "__main__":
    server = ThreadedServer(remoteServer, port = 12345)
    server.start()

最佳答案

我会用

class RedirectingService(rpyc.Service):
    def exposed_redirect(self, stdout):
        sys.stdout = stdout
    def exposed_restore(self):
        sys.stdout = sys.__stdout__

然后

c = rpyc.connect("server", config={"allow_public_attrs":True})
c.redirect(sys.stdout)
...
c.restore()

看看http://rpyc.readthedocs.org/en/latest/api/core_protocol.html#rpyc.core.protocol.DEFAULT_CONFIG对于配置选项。经典模式基本上将所有内容都设置为 True,这可能不安全,但您可以控制要显式设置的选项。

关于python - 在不使用经典 RPyC 的情况下将 RPyC 的标准输出重定向到本地客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23892351/

相关文章:

c - 基于结束条件的 C 格式输出

bash - 如何将ssh作业发送到后台

bash - 为什么在 bash while 循环中使用 "<&3"、 "3&-"和 "3</file"?它实际上有什么作用?

Python 子进程和 shell 输入重定向

python - pyparsing:忽略注释时,parseAll=True 不会抛出 ParseException

c - 使用 stdout 和 stderr 禁用缓冲是否安全?

python - 计算非常小的值的-log10

c - 查看后台进程的输出

Python 正则表达式获取所有内容,直到像 ''(年)这样的表达式”

Python 将变量从一个脚本发送到另一个脚本