一、简单介绍

为了解决在系统的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. robotframework自动化系列:随机下拉框

    robotframework自动化系列:随机下拉框 随着项目自动化不断推进,在下拉框定位的时候出现些问题,每次下拉框选择都是相同的下拉选项,如果想每次选择的选项不一样,该如何实现呢,查找了很多资料,没 ...

  2. 利用C#转换图片格式及转换为ico

    注意:转换为ICO后效果不好. 源代码: using System;using System.Collections.Generic;using System.Text;using System.Dr ...

  3. man ctags

    ctags命令帮助   命令格式     ctags [options] [file(s)] 或     etags [options] [file(s)]   源文件参数     不同语言中对象的种 ...

  4. SpringBoot之彩色输出

    spring.output.ansi.enabled NEVER:禁用ANSI-colored输出(默认项) DETECT:会检查终端是否支持ANSI,是的话就采用彩色输出(推荐项) ALWAYS:总 ...

  5. unique & lower_bound C++

    原来C++也有unique和lower_bound,只需头文件iostream unique unique可以对数组进行相邻元素的"去重",实现效果是把所有不重复的元素按顺序放在数 ...

  6. laravel5.3统计 withCount()方法的使用

    在laravel5.3之后可以使用withCount()这个方法. 注意:一定要是5.3版本之后,5.2和5.1都会报方法未定义 举个栗子: App\Post::withCount('comments ...

  7. LINQ学习系列-----1.3 扩展方法

    这篇内容继续接着昨天的Lambda表达式的源码继续下去.昨天讲了Lambda表达式,此篇讲扩展方法,这两点都是Linq带来的新特性.    一.扩展方法介绍   废话不多说,先上源码截图: 上图中Ge ...

  8. 加密代理和Retrofit解密Converter

    最近在研究安卓的Retrofit框架,服务器的数据全部用加密算法加密了,发现无法使用"com.squareup.retrofit2:converter-gson:2.1.0"Jar ...

  9. 编写带对话框界面的OCX

    编写带对话框界面的OCX步骤: 1.添加Dialog资源,切换到资源视图,将对话框的Style设置为Child,在对话框界面右击添加类,输入类名MyDlg,使得其继承与CDialogEx.(继承CDi ...

  10. VMware虚拟机安装教程

    在实际的开发过程中,使用到VMware的时候是很多的.当你的电脑安装的是windows系统而想使用linux系统时,为了避免对本机的系统进行操作,那么安装虚拟机就是一项不错的选择. 在写这篇博文时,刚 ...