一、简单介绍

为了解决在系统的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. jQuery选择器(子元素过滤选择器)第七节

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. HTML学习笔记 CSS文本及字体及连接及列表(a标签使用及缩进) 案例 第七节 (原创)参考使用表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Nginx实现负载均衡&Nginx缓存功能

    一.Nginx是什么 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambl ...

  4. 基础教程:ASP.NET Core 2.0 MVC筛选器

    问题 如何在ASP.NET Core的MVC请求管道之前和之后运行代码. 解 在一个空的项目中,更新 Startup 类以添加MVC的服务和中间件. publicvoid ConfigureServi ...

  5. Verilog中变量位宽注意

    Verilog中,变量定义方式可以为:reg[位宽-1:0] 数据名:reg[位宽:1] 数据名.其他变量也类似. 以reg变量cnt为例,当cnt位宽为4时,可定义为reg[3:0] cnt,或者定 ...

  6. Nginx 搭建图片服务器

    Nginx 搭建图片服务器 本章内容通过Nginx 和 FTP 搭建图片服务器.在学习本章内容前,请确保您的Linux 系统已经安装了Nginx和Vsftpd. Nginx 安装:http://www ...

  7. Less合并

    合并是LESS的一个特性,它允许通过指定的语法来为某个属性添加使用逗号或空格分隔的值的列表.对于文本阴影.盒阴影.背景.变换等允许使用值的列表的属性,合并非常有用. 合并的语法,就是在属性名称和冒号之 ...

  8. mysql安装简单教程(自动安装/配置安装)

    mysql安装简单教程(自动安装/配置安装) 1.1前言: 由于特殊原因,在最近2-3个月里mysql真是安装了无数遍,每次安装都要上网找教程,每个教程基本都不一样,因此还是自己写下来比较好,毕竟自己 ...

  9. javascript的BOM,DOM对象

    BOM对象 window对象 所有浏览器都支持 window 对象.概念上讲.一个html文档对应一个window对象.功能上讲: 控制浏览器窗口的.使用上讲: window对象不需要创建对象,直接使 ...

  10. SPOJ SERGRID - Grid BFS

    SERGRID - Grid no tags  You are on an nxm grid where each square on the grid has a digit on it. From ...