【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/


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

查看所有标签

猜你喜欢:

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

Hacker's Delight

Hacker's Delight

Henry S. Warren Jr. / Addison-Wesley / 2002-7-27 / USD 59.99

A collection useful programming advice the author has collected over the years; small algorithms that make the programmer's task easier. * At long last, proven short-cuts to mastering difficult aspec......一起来看看 《Hacker's Delight》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具