Python XML RPC服务器端和客户端实例
作者:junjie 时间:2022-07-26 05:41:57
一、远程过程调用RPC
XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data. This module supports writing XML-RPC client code; it handles all the details of translating between conformable Python objects and XML on the wire.
简单地,client可以调用server上提供的方法,然后得到执行的结果。类似与webservice。
推荐查看xmlprc的源文件:C:\Python31\Lib\xmlrpc
二、实例
1) Server
from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler
def div(x,y):
return x - y
class Math:
def _listMethods(self):
# this method must be present for system.listMethods
# to work
return ['add', 'pow']
def _methodHelp(self, method):
# this method must be present for system.methodHelp
# to work
if method == 'add':
return "add(2,3) => 5"
elif method == 'pow':
return "pow(x, y[, z]) => number"
else:
# By convention, return empty
# string if no help is available
return ""
def _dispatch(self, method, params):
if method == 'pow':
return pow(*params)
elif method == 'add':
return params[0] + params[1]
else:
raise 'bad method'
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_introspection_functions()
server.register_function(div,"div")
server.register_function(lambda x,y: x*y, 'multiply')
server.register_instance(Math())
server.serve_forever()
2)client
import xmlrpc.client
s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.system.listMethods())
print(s.pow(2,3)) # Returns 28
print(s.add(2,3)) # Returns 5
print(s.div(3,2)) # Returns 1
print(s.multiply(4,5)) # Returns 20
3)result
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python编程实现数学运算求一元二次方程的实根算法示例
![](https://img.aspxhome.com/file/2023/4/62254_0s.jpg)
MySQL过滤数据操作方法梳理
python实现桌面气泡提示功能
![](https://img.aspxhome.com/file/2023/0/94070_0s.jpg)
高性能网站建设指南:性能提升的14个原则
Docker安装MySQL8.0的实现方法
![](https://img.aspxhome.com/file/2023/1/128001_0s.jpg)
python 如何把classification_report输出到csv文件
![](https://img.aspxhome.com/file/2023/1/131581_0s.jpg)
教你使用Sublime text3搭建Python开发环境及常用插件安装另分享Sublime text3最新激活注册码
![](https://img.aspxhome.com/file/2023/0/109360_0s.png)
python中leastsq函数的使用方法
![](https://img.aspxhome.com/file/2023/3/108153_0s.png)
python命令行参数解析OptionParser类用法实例
![](https://img.aspxhome.com/file/2023/6/70736_0s.gif)
python批量处理txt文件的实例代码
![](https://img.aspxhome.com/file/2023/8/117958_0s.jpg)
Python数据分析 Pandas Series对象操作
![](https://img.aspxhome.com/file/2023/4/99664_0s.png)
解析:快速的掌握 MySQL支持的操作系统
python如何读取bin文件并下发串口
python爬虫容易学吗
Python中的测试模块unittest和doctest的使用教程
python分析网页上所有超链接的方法
原生js实现对Ajax的封装(仿jquery)
Sun正式发布MySQL 5.1版 简化数据库应用
微信小程序实现上传图片功能
![](https://img.aspxhome.com/file/2023/6/129596_0s.jpg)
Jenkins使用publish html report插件展示HTML报告的方法
![](https://img.aspxhome.com/file/2023/0/132300_0s.png)