Python远程过程调用(无远程部分) [英] Python Remote Procedure Call (Without the Remote Part)

查看:234
本文介绍了Python远程过程调用(无远程部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不是以root身份运行的Python服务器,它位于我正在开发的应用程序的前面.但是,有些应用程序功能需要访问RAW套接字,这意味着具有root特权.

I have a Python server which is not running as root, which fronts an application I am developing. However there are some application features which require access to RAW sockets which means root privileges.

很明显,我不想以root用户身份运行主服务器,因此我的解决方案是创建一个以root用户身份运行的守护进程或命令行脚本,以提供对所述功能的保护访问.

Obviously I do not want to run the main server as root, and so my solution is to create a daemon process or command line script which runs as root providing guarded access to said features.

但是我想搁置stdin/stdout通信并使用RPC交互方式,例如 Pyro .但这会将RPC接口公开给可以通过网络访问该计算机的任何人,而我知道调用RPC方法的进程将是同一台计算机上的另一个进程.

However I want put aside stdin/stdout communication and use an RPC style of interaction such as Pyro. But this exposes the RPC interface to anyone with network access to the machine, whereas I know that the process calling the RPC methods will be another process on the same machine.

是否没有一种可以以类似的方式(仅适用于本地计算机)使用的进程间过程调用标准?我想象服务器正在做这样的事情:

Is there not a sort of inter-process procedure call standard which could be used in a similar (local machine only) fashion? I imagine the server doing something like this:

# Server not running as root
pythonically, returned, values = other_process_running_as_root.some_method()

以root身份运行的进程公开了一个方法:

And the process running as root exposing a method:

# Daemon running as root
@expose_this_method
def some_method():
    # Play with RAW sockets
    return pythonically, returned, values

像这样可能吗?

推荐答案

在我发表评论之后,我很想知道是否有可能,所以我尝试将其组合在一起:

Following my comment, I was interested to see if it was possible, so I had a go at putting this together: https://github.com/takowl/ZeroRPC

请记住,这是在一个小时左右的时间内一起抛出的,因此,几乎可以肯定它不如任何严肃的解决方案(例如,服务器端的任何错误都会使其崩溃...).但这按您的建议起作用:

Bear in mind that this is thrown together in an hour or so, so it's almost certainly inferior to any serious solution (e.g. any errors on the server side will crash it...). But it works as you suggested:

服务器:

rpcserver = zerorpc.Server("ipc://myrpc.ipc")

@rpcserver.expose
def product(a, b):
    return a * b

rpcserver.run()

客户:

rpcclient = zerorpc.Client("ipc://myrpc.ipc")

print(rpcclient.product(5, 7))
rpcclient._stopserver()

这篇关于Python远程过程调用(无远程部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆