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的垃圾回收机制深入分析
Jul 16 Python
简单谈谈python中的多进程
Nov 06 Python
Python绑定方法与非绑定方法详解
Aug 18 Python
Python中的is和==比较两个对象的两种方法
Sep 06 Python
浅谈numpy库的常用基本操作方法
Jan 09 Python
DataFrame中去除指定列为空的行方法
Apr 08 Python
Python使用pickle模块实现序列化功能示例
Jul 13 Python
Python双向循环链表实现方法分析
Jul 30 Python
关于python列表增加元素的三种操作方法
Aug 22 Python
pytorch之Resize()函数具体使用详解
Feb 27 Python
在jupyter notebook中调用.ipynb文件方式
Apr 14 Python
解决Python import .pyd 可能遇到路径的问题
Mar 04 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实现批量查询清除一句话后门的代码
2008/01/20 PHP
隐性调用php程序的方法
2009/03/09 PHP
ThinkPHP框架获取最后一次执行SQL语句及变量调试简单操作示例
2018/06/13 PHP
php pdo连接数据库操作示例
2019/11/18 PHP
写得不错的jquery table鼠标经过变色代码
2013/09/27 Javascript
比较不错的JS/JQuery显示或隐藏文本的方法
2014/02/13 Javascript
css+js实现部分区域高亮可编辑遮罩层
2014/03/04 Javascript
jQuery获取样式中颜色值的方法
2015/01/29 Javascript
JS实现的鼠标跟随代码(卡通手型点击效果)
2015/10/26 Javascript
jQuery Mobile操作HTML5的常用函数总结
2016/05/17 Javascript
详解微信小程序开发之下拉刷新 上拉加载
2016/11/24 Javascript
JS验证不重复验证码
2017/02/10 Javascript
详解基于node的前端项目编译时内存溢出问题
2017/08/01 Javascript
Vue组件之Tooltip的示例代码
2017/10/18 Javascript
详述 Sublime Text 打开 GBK 格式中文乱码的解决方法
2017/10/26 Javascript
bootstrap响应式导航条模板使用详解(含下拉菜单,弹出框)
2017/11/17 Javascript
深入了解响应式React Native Echarts组件
2019/05/29 Javascript
Node.js从字符串生成文件流的实现方法
2019/08/18 Javascript
vue实现分页加载效果
2019/12/24 Javascript
vue集成一个支持图片缩放拖拽的富文本编辑器
2021/01/29 Vue.js
[01:15]《辉夜杯》北京网鱼队巡礼
2015/10/26 DOTA
详解python 模拟豆瓣登录(豆瓣6.0)
2019/04/18 Python
python批量下载抖音视频
2019/06/17 Python
python用match()函数爬数据方法详解
2019/07/23 Python
使用Python获取当前工作目录和执行命令的位置
2020/03/09 Python
html5实现多图片预览上传及点击可拖拽控件
2018/03/15 HTML / CSS
棉花姑娘教学反思
2014/02/15 职场文书
《陈毅探母》教学反思
2014/05/01 职场文书
校园文明倡议书
2014/05/16 职场文书
工程学毕业生自荐信
2014/06/14 职场文书
小学运动会演讲稿
2014/08/25 职场文书
新闻发布会活动策划方案
2014/09/15 职场文书
党员四风问题对照检查材料
2014/09/27 职场文书
店铺转让协议书
2014/12/02 职场文书
精神文明建设先进个人事迹材料
2014/12/24 职场文书
仓库管理员岗位职责
2015/02/03 职场文书