【Python 3.x】xmlrpc模块使用教程

栏目: Python · 发布时间: 8年前

内容简介:【Python 3.x】xmlrpc模块使用教程

RPC 是远程过程调用, XML-RPC 是一种使用 xml 文本的方式利用 http 协议传输命令和数据的 RPC 机制。

Python 3.x 中,我们也可以使用 xmlrpc 模块编写程序,让该程序与可与其它任何语言编写的 XML-RPC 服务器实现远程过程调用。

服务端编写

开启一个终端,输入 ipython ,然后输入下面的服务端代码:

from xmlrpc.server import SimpleXMLRPCServer

server = SimpleXMLRPCServer(("localhost", 8080), allow_none=True)

上述代码创建了一个 server 服务端,接下来,输入:

def hello():  
    print('Hello, I\'m Server.')
server.register_function(hello)

server 注册一个函数 hello ,等待客户端调用,最后:

server.serve_forever()

运行 server ,令其监听在 8000 端口。

客户端编写

新开一个终端,输入 ipython ,开始编写客户端代码:

from xmlrpc.client import ServerProxy

sp = ServerProxy("http://localhost:8080")

# 调用server的hello
sp.hello()

接下来,转到 server 的终端,可以看到 Hello, I'm Server. 的输出。

以上只是抛砖引玉。

想了解 xmlrpc 的更多用法,可通过下面的代码:

from xmlrpc import server,client

help(server)  
help(client)

注释非常详细。

转载请注明出处

http://www.zgljl2012.com/python-3-x-xmlrpcmo-kuai-shi-yong-jiao-cheng/


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Pro Git (Second Edition)

Pro Git (Second Edition)

Scott Chacon、Ben Straub / Apress / 2014-11-9 / USD 59.99

Scott Chacon is a cofounder and the CIO of GitHub and is also the maintainer of the Git homepage ( git-scm.com ) . Scott has presented at dozens of conferences around the world on Git, GitHub and the ......一起来看看 《Pro Git (Second Edition)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试