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的另外几种语言实现
Jan 29 Python
详细探究Python中的字典容器
Apr 14 Python
python实现自动重启本程序的方法
Jul 09 Python
Python断言assert的用法代码解析
Feb 03 Python
如何使用VSCode愉快的写Python于调试配置步骤
Apr 06 Python
基于python神经卷积网络的人脸识别
May 24 Python
Python中的异常处理try/except/finally/raise用法分析
Feb 28 Python
Django给admin添加Action的步骤详解
May 01 Python
python创建子类的方法分析
Nov 28 Python
Python导入模块包原理及相关注意事项
Mar 25 Python
Python字节单位转换(将字节转换为K M G T)
Mar 02 Python
python 将Excel转Word的示例
Mar 02 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
使用 php4 加速 web 传输
2006/10/09 PHP
php类
2006/11/27 PHP
PHP 反射机制实现动态代理的代码
2008/10/22 PHP
很好用的PHP数据库类
2009/05/27 PHP
PHP使用strtotime计算两个给定日期之间天数的方法
2015/03/18 PHP
给WordPress中的留言加上楼层号的PHP代码实例
2015/12/14 PHP
PHP构造函数与析构函数用法示例
2016/09/28 PHP
PHP7 新增功能
2021/03/09 PHP
js中的值类型和引用类型小结 文字说明与实例
2010/12/12 Javascript
在ASP.NET中使用JavaScript脚本的方法
2013/11/12 Javascript
js图片处理示例代码
2014/05/12 Javascript
IE8中动态创建script标签onload无效的解决方法
2014/12/22 Javascript
使用伪命名空间封装保护独自创建的对象方法
2016/08/04 Javascript
ES6新特性之变量和字符串用法示例
2017/04/01 Javascript
JS实现获取图片大小和预览的方法完整实例【兼容IE和其它浏览器】
2017/04/24 Javascript
详解有关easyUI的拖动操作中droppable,draggable用法例子
2017/06/03 Javascript
jQuery EasyUI Layout实现tabs标签的实例
2017/09/26 jQuery
vue2.0+koa2+mongodb实现注册登录
2018/04/10 Javascript
Angular 利用路由跳转到指定页面的指定位置方法
2018/08/31 Javascript
vue项目上传Github预览的实现示例
2018/11/06 Javascript
详解vue中async-await的使用误区
2018/12/05 Javascript
JS阻止事件冒泡的方法详解
2019/08/26 Javascript
Vue Object 的变化侦测实现代码
2020/04/15 Javascript
详解Nuxt内导航栏的两种实现方式
2020/04/16 Javascript
详细分析React 表单与事件
2020/07/08 Javascript
python处理文本文件并生成指定格式的文件
2014/07/31 Python
详解设计模式中的工厂方法模式在Python程序中的运用
2016/03/02 Python
使用 Python 实现微信群友统计器的思路详解
2018/09/26 Python
python后端接收前端回传的文件方法
2019/01/02 Python
关于python之字典的嵌套,递归调用方法
2019/01/21 Python
Python绘制堆叠柱状图的实例
2019/07/09 Python
pycharm 设置项目的根目录教程
2020/02/12 Python
优秀员工年终发言演讲稿
2014/01/01 职场文书
老公爱的承诺书
2014/03/31 职场文书
大学辅导员述职报告
2015/01/10 职场文书
导游词之平津战役纪念馆
2019/11/04 职场文书