- Python - 网络编程
- Python - 网络基础
- Python - 网络环境
- Python - 互联网协议
- Python - IP 地址
- Python - DNS 查询
- Python - 路由
- Python - HTTP 请求
- Python - HTTP 响应
- Python - HTTP 头部
- Python - 自定义 HTTP 请求
- Python - 请求状态码
- Python - HTTP 认证
- Python - HTTP 数据下载
- Python - 连接重用
- Python - 网络接口
- Python - 套接字编程
- Python - HTTP 客户端
- Python - HTTP 服务器
- Python - 构建 URL
- Python - Web 表单提交
- Python - 数据库和 SQL
- Python - Telnet
- Python - 邮件消息
- Python - SMTP
- Python - POP3
- Python - IMAP
- Python - SSH
- Python - FTP
- Python - SFTP
- Python - Web 服务器
- Python - 数据上传
- Python - 代理服务器
- Python - 目录列表
- Python - 远程过程调用
- Python - RPC JSON 服务器
- Python - Google 地图
- Python - RSS Feed
Python - 远程过程调用
远程过程调用 (RPC) 系统允许您使用与在本地库中调用函数相同的语法来调用远程服务器上的可用函数。这在两种情况下很有用。
- 您可以利用来自多台机器的处理能力使用 rpc,而无需更改调用远程系统中程序的代码。
- 处理所需的数据仅在远程系统中可用。
因此,在 Python 中,我们可以将一台机器视为服务器,另一台机器视为客户端,客户端将向服务器发出调用以运行远程过程。在我们的示例中,我们将使用本地主机并将其用作服务器和客户端。
运行服务器
Python 语言自带一个内置服务器,我们可以将其作为本地服务器运行。运行此服务器的脚本位于 Python 安装的 bin 文件夹下,名为 classic.py。我们可以在 Python 提示符下运行它并检查它是否作为本地服务器运行。
python bin/classic.py
当我们运行上述程序时,我们得到以下输出 -
INFO:SLAVE/18812:server started on [127.0.0.1]:18812
运行客户端
接下来,我们使用 rpyc 模块运行客户端以执行远程过程调用。在下面的示例中,我们在远程服务器上执行 print 函数。
import rpyc
conn = rpyc.classic.connect("localhost")
conn.execute("print('Hello from Tutorialspoint')")
当我们运行上述程序时,我们得到以下输出 -
Hello from Tutorialspoint
通过 RPC 进行表达式求值
使用上述代码示例,我们可以使用 Python 的内置函数通过 rpc 执行和评估表达式。
import rpyc
conn = rpyc.classic.connect("localhost")
conn.execute('import math')
conn.eval('2*math.pi')
当我们运行上述程序时,我们得到以下输出 -
6.283185307179586
广告