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中用Descriptor实现类级属性(Property)详解
Sep 18 Python
Python生成器(Generator)详解
Apr 13 Python
使用Python的Zato发送AMQP消息的教程
Apr 16 Python
Python中使用装饰器来优化尾递归的示例
Jun 18 Python
Python编程实现蚁群算法详解
Nov 13 Python
Python读取视频的两种方法(imageio和cv2)
Apr 15 Python
基于numpy.random.randn()与rand()的区别详解
Apr 17 Python
Python循环结构的应用场景详解
Jul 11 Python
python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法实例
Feb 26 Python
django中嵌套的try-except实例
May 21 Python
通过实例解析python and和or使用方法
Nov 14 Python
python实现调用摄像头并拍照发邮箱
Apr 27 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 之Section与Cookie使用总结
2012/09/14 PHP
PHP实现的简单日历类
2014/11/29 PHP
php基于PDO连接MSSQL示例DEMO
2016/07/13 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
PHP7原生MySQL数据库操作实现代码
2020/07/03 PHP
Prototype Hash对象 学习
2009/07/19 Javascript
一些经常会用到的Javascript检测函数
2010/05/31 Javascript
php图像生成函数之间的区别分析
2012/12/06 Javascript
javascript中SetInterval与setTimeout的定时器用法
2015/08/24 Javascript
JavaScript File分段上传
2016/03/10 Javascript
深入浅析JS的数组遍历方法(推荐)
2016/06/15 Javascript
js判断radiobuttonlist的选中值显示/隐藏其它模块的实现方法
2016/08/25 Javascript
微信小程序 Storage API实例详解
2016/10/02 Javascript
微信小程序开发之Tabbar实例详解
2017/01/09 Javascript
微信小程序倒计时功能实现代码
2017/11/09 Javascript
vue-cli3脚手架的配置及使用教程
2018/08/28 Javascript
python操作日期和时间的方法
2014/03/11 Python
python字典序问题实例
2014/09/26 Python
python编程开发之textwrap文本样式处理技巧
2015/11/13 Python
详解Python读取配置文件模块ConfigParser
2017/05/11 Python
Django实现快速分页的方法实例
2017/10/22 Python
python实现词法分析器
2019/01/31 Python
scrapy处理python爬虫调度详解
2020/11/23 Python
利用CSS3参考手册和CSS3代码生成工具加速来学习网页制
2012/07/11 HTML / CSS
美国隐形眼镜销售网站:ContactsDirect
2017/10/28 全球购物
100%植物性、有机、即食餐:Sakara Life
2018/10/25 全球购物
文件中有一组整数,要求排序后输出到另一个文件中
2012/01/04 面试题
药学专业个人的自我评价
2013/12/31 职场文书
中班中秋节活动反思
2014/02/18 职场文书
英文请假条
2014/04/11 职场文书
王兆力在市委党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
2014年涉外离婚协议书范本
2014/11/20 职场文书
2014年测量员工作总结
2014/12/12 职场文书
学雷锋团日活动总结
2015/05/06 职场文书
《自然之道》读后感3篇
2019/12/17 职场文书
CSS3 制作的书本翻页特效
2021/04/13 HTML / CSS