Python XML RPC服务器端和客户端实例


Posted in Python onNovember 22, 2014

一、远程过程调用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

Python XML RPC服务器端和客户端实例

Python 相关文章推荐
python中split方法用法分析
Apr 17 Python
Python中的推导式使用详解
Jun 03 Python
Python实现带参数与不带参数的多重继承示例
Jan 30 Python
python使用代理ip访问网站的实例
May 07 Python
python 将list转成字符串,中间用符号分隔的方法
Oct 23 Python
Python版名片管理系统
Nov 30 Python
django使用LDAP验证的方法示例
Dec 10 Python
使用django实现一个代码发布系统
Jul 18 Python
Python 将 QQ 好友头像生成祝福语的实现代码
May 03 Python
Python3基于plotly模块保存图片表格
Aug 03 Python
Python logging模块原理解析及应用
Aug 13 Python
python温度转换华氏温度实现代码
Dec 06 Python
Python实现读取目录所有文件的文件名并保存到txt文件代码
Nov 22 #Python
python进程类subprocess的一些操作方法例子
Nov 22 #Python
Python读取环境变量的方法和自定义类分享
Nov 22 #Python
Python中的引用和拷贝浅析
Nov 22 #Python
python实现的文件夹清理程序分享
Nov 22 #Python
Python判断操作系统类型代码分享
Nov 22 #Python
python logging类库使用例子
Nov 22 #Python
You might like
用PHP的超级变量$_GET获取HTML表单(Form) 数据
2011/05/07 PHP
php使用curl检测网页是否被百度收录的示例分享
2014/01/31 PHP
php计划任务之ignore_user_abort函数实现方法
2015/01/08 PHP
PHP的APC模块实现上传进度条
2015/10/27 PHP
PHP如何通过AJAX方式实现登录功能
2015/11/23 PHP
Yii2单元测试用法示例
2016/11/12 PHP
JavaScript 事件冒泡简介及应用
2010/01/11 Javascript
原生Js实现按的数据源均分时间点幻灯片效果(已封装)
2010/12/28 Javascript
JS获得URL超链接的参数值实例代码
2013/06/21 Javascript
JQueryiframe页面操作父页面中的元素与方法(实例讲解)
2013/11/19 Javascript
jQuery实现下拉框左右选择的简单实例
2014/02/22 Javascript
jQuery中andSelf()方法用法实例
2015/01/08 Javascript
JavaScript操作Oracle数据库示例
2015/03/06 Javascript
jquery实现红色竖向多级向右展开的导航菜单效果
2015/08/31 Javascript
全面详细的jQuery常见开发技巧手册
2016/02/21 Javascript
JavaScript编写带旋转+线条干扰的验证码脚本实例
2016/05/30 Javascript
IE11下使用canvas.toDataURL报SecurityError错误的解决方法
2017/11/19 Javascript
通过js动态创建标签,并设置属性方法
2018/02/24 Javascript
vue自定义底部导航栏Tabbar的实现代码
2018/09/03 Javascript
js实现GIF动图分解成多帧图片上传
2019/10/24 Javascript
如何基于layui的laytpl实现数据绑定的示例代码
2020/04/10 Javascript
Python中使用logging模块打印log日志详解
2015/04/05 Python
Python 绘图和可视化详细介绍
2017/02/11 Python
Python实现的字典排序操作示例【按键名key与键值value排序】
2018/12/21 Python
python开发游戏的前期准备
2019/05/05 Python
python3实现猜数字游戏
2020/12/07 Python
Django model select的多种用法详解
2019/07/16 Python
django xadmin中form_layout添加字段显示方式
2020/03/30 Python
什么是URL
2015/12/13 面试题
班班通项目实施方案
2014/02/25 职场文书
2015双创工作总结
2015/07/24 职场文书
保护环境建议书作文400字
2015/09/14 职场文书
嵌入式Redis服务器在Spring Boot测试中的使用教程
2021/07/21 Redis
PC版《死亡搁浅导剪版》现已发售 展开全新的探险
2022/04/03 其他游戏
redis 解决库存并发问题实现数量控制
2022/04/08 Redis
Python pyecharts案例超市4年数据可视化分析
2022/08/14 Python