一、简单介绍

为了解决在系统的80port提供RPC的服务。而又不影响正在运行的WEB服务。人们想出了用HTTP协议传输RPC包的办法。对于差点儿是专门用于传输文本的HTTP协议。要在其上传输RPC封包。最方便的方法莫过于把RPC封包编码成文本形式——比如XML文件。

XML- RPC(http://www.xml-rpc.com)是由美国UserLand公司指定的一个RPC协议。它将RPC信息封包编码为XML,然后通过 HTTP传输封包;

 简单的理解:

将数据定义为xml格式。通过http协议进行远程传输。

二、优点

1. 传输复杂的数据。

2. 通过程序语言的封装,实现远程对象的调用。

三、Python中xmlrpc应用

server端:

from SimpleXMLRPCServer import *
#import SimpleXMLRPCRequestHandler def div(x,y):
return x -y class Math:
def _listMethods(self):
return ['add','pow'] def _methodHelp(self,method):
if method == 'add':
return "add(2,3) =>5"
elif method == 'pow':
return "pow(x,y[,z])=>number"
else:
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))
# register_introspection_functions:Registers the XML-RPC introspection methods in the system namespace
server.register_introspection_functions()
server.register_function(div,"div")
server.register_function(lambda x,y:x*y,'multiply')
server.register_instance(Math())
#serve_forever:Handle one request at a time until shutdown.
#Polls for shutdown every poll_interval seconds. Ignores
#self.timeout. If you need to do periodic tasks, do them in
#another thread.
server.serve_forever()

client:

import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:8000')
print(s.system.listMethods()) print(s.pow(2,3))
print(s.add(2,3))
print(s.div(3,2))
print(s.multiply(4,5))

执行情况:

[maokx@maokexu1 xmlrpc]$ python server.py

 /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:37] "POST /RPC2 HTTP/1.0" 200 - /usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 -
/usr/lib64/python2.6/rfc822.py, readheaders, 131, enter readheaders
/usr/lib64/python2.6/rfc822.py, readheaders, 205, exit readheaders
localhost.localdomain - - [16/Nov/2014 23:25:45] "POST /RPC2 HTTP/1.0" 200 - [maokx@maokexu1 xmlrpc]$ python client.py
['add', 'div', 'multiply', 'pow', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']
8
5
1
20
[maokx@maokexu1 xmlrpc]$ python client.py
['add', 'div', 'multiply', 'pow', 'system.listMethods', 'system.methodHelp', 'system.methodSignature']
8
5
1
20
[maokx@maokexu1 xmlrpc]$



Python的招牌菜xmlrpc的更多相关文章

  1. Python与PHP通过XMLRPC进行通信

    Python与PHP通过XMLRPC进行通信:服务器端用Python,客户端用PHP. 服务器端:xmlrpc_server.py #!/usr/bin/python # coding: UTF-8 ...

  2. The Python web services developer: XML-RPC for Python

    原文地址:http://www.ibm.com/developerworks/webservices/library/ws-pyth10/index.html 摘要:概括地说,您可以将 XML-RPC ...

  3. linux 下 rpc python 实例之使用XML-RPC进行远程文件共享

    这是个不错的练习,使用python开发P2P程序,或许通过这个我们可以自己搞出来一个P2P下载工具,类似于迅雷.XML-RPC是一个远程过程调用(remote procedure call,RPC)的 ...

  4. Python 手册——开胃菜

    如果你写过大规模的Shell脚本,应该会有过这样的体会:你还非常想再加一些别的功能进去,但它已经太大. 太慢.太复杂了:或者这个功能需要调用一个系统函数,或者它只适合通过C来调用……通常这些问题还不足 ...

  5. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  6. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  7. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  8. 自学python笔记(一)

    一   简介:Python是著名的"龟叔"Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言.其他的就不说了..... python是 ...

  9. 零基础学习python(2)

    再讲新知识之前,先将一些之前没提的东西再介绍一下: (1) 命令行模式 在Windows开始菜单选择“命令提示符”(或者是在搜索栏中输入“cmd”),就进入到命令行模式,它的提示符类似C:\>: ...

随机推荐

  1. Problem F: 合唱比赛开始了!

    Problem F: 合唱比赛开始了! Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 201[Submit][Status][ ...

  2. webapp通用选择器:iosselect

    1,这个组件解决什么问题 在IOS系统中,safari浏览器的select标签默认展示样式和iOS-UIPickerView展示方式一致,形如下图: 这个选择器操作方便,样式优美.但是在安卓系统中展示 ...

  3. [转载] 应用于负载均衡的一致性哈希及java实现

    转载自http://blog.csdn.net/haitao111313/article/details/7537799 这几天看了几遍一致性哈希的文章,但是都没有比较完整的实现,因此试着实现了一下, ...

  4. python基础(一)------Python基础语法与介绍

    编程语言的历史和Python开发 一.编程语言 1.编程语言也是"语言"与英语,汉语等类似,掌握其语法结构,灵活 的运用其语法规则为之重要.          编程语言实现的是程序 ...

  5. swizzle method 和消息转发机制的实际使用

    我的工程结构,如图 1-0 图  1-0 在看具体实现以前,先捋以下 实现思路. ViewController 中有一个-(void)Amethod;A方法. -(void)Amethod{ NSLo ...

  6. linux配置远程Git仓库

    一,安装git yum install git 二,在服务器(119.28.1.1)目录( /git/admin )上创建一个仓库 cd /git/admin touch aaa.html git i ...

  7. 机器学习(二)-kNN手写数字识别

    一.kNN算法是机器学习的入门算法,其中不涉及训练,主要思想是计算待测点和参照点的距离,选取距离较近的参照点的类别作为待测点的的类别. 1,距离可以是欧式距离,夹角余弦距离等等. 2,k值不能选择太大 ...

  8. Openning

    In order to imporve my english writing skill and enhance my understanding of  programming ,I'm setti ...

  9. css实现六边形图片(最简单易懂方法实现高逼格图片展示)

    不说别的,先上效果: 用简单的div配合伪元素,即可‘画出’这幅六边形图片,原理是三个相同宽高的div,通过定位旋转拼合成一个六边形,再利用背景图层叠,形成视觉上的一张整图.下面咱们一步一步来实现. ...

  10. JAVA基础4——谈谈HashCode与HashMap相关概念

    谈谈HashCode与HashMap HashCode hashCode,即一个Object的散列码. HashCode的作用: 对于List.数组等集合而言,HashCode用途不大: 对于Hash ...