一、简单介绍

为了解决在系统的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. P3003 [USACO10DEC]苹果交货Apple Delivery

    题目描述 Bessie has two crisp red apples to deliver to two of her friends in the herd. Of course, she tr ...

  2. robotframework自动化系列:新增流程

    刚接手项目的时候,要求所有流程在上线之前必须确保正向操作是正确的:这个时候又有新的模块需要测试,所以引入自动化测试是非常有必要的!通过对比,尝试使用RF进行自动化的回归测试. 测试中最常见的操作就是增 ...

  3. 通过geotools读写shp文件

    依赖jar包 读取shp public static void main(String[] path) { DbaseFileReader reader = null; try { reader = ...

  4. Navi.Soft31.产品.登录器(永久免费)

    1系统简介 1.1功能简述 电商平台和传统店铺相比,确实方便不少,直接在网上下单,快递直接送货到家.这其中,做电商平台的童鞋表示压力很大,因为可能同时开很多店铺,每个店铺都要登录.查看订单量.发货拣货 ...

  5. 前后端分手大师——MVVM 模式

    之前对 MVVM 模式一直只是模模糊糊的认识,正所谓没有实践就没有发言权,通过这两年对 Vue 框架的深入学习和项目实践,终于可以装B了有了拨开云雾见月明的感觉. 简而言之 Model–View–Vi ...

  6. CLR设计类型之接口

    写在前面的话:             写到这一节的时候,CLR设计类型就已经结束了,因为CLR要求的是有一定基础的人看的,所以我们不是从基础类型以及运算符开始的,文章从一开始就讲的是深入面向对象编程 ...

  7. [转]如何监测谁用了SQL Server的Tempdb空间

    Tempdb 系统数据库是一个全局资源,供连接到 SQL Server 实例的所有用户使用.在现在的SQL Server里,其使用频率可能会超过用户的想象.如果Tempdb空间耗尽,许多操作将不能完成 ...

  8. bootstrap的模态简单案例

    使用时需添加bootstrap的引用,否则实现不出来效果 <!DOCTYPE html> <html><head>    <meta name="v ...

  9. python打包exe pyinstaller 简单使用

    源由 最近公司让做了一个小工具,使用python写的,写完之后要求能放在其它电脑上运行,于是就开始寻找方案; 按网上的说法 py2exe已经很久没更新了,资料也不多: 于是就采用pyinstaller ...

  10. head first python菜鸟学习笔记(第七章) ——web应用之为数据建模

    问题1. #意思是从athletelist.py中导入AthleteListfrom athletelist import AthleteList 源程序代码 import pickle from a ...