rpc框架 SimpleXMLRP

一台电脑去控制另一台电脑干事情,最常见的就是web。

用户get/post一个url,服务端根据url地址和参数去做相应的事情把结果返回给客户端。

server.py

#coding=utf8
from SimpleXMLRPCServer import SimpleXMLRPCServer
import time

def happy(x):
    return time.strftime('%H:%M:%S'),"I play Python.! 
 x is %s"%x

sxr=SimpleXMLRPCServer(("", 8080), allow_none=True)
sxr.register_function(happy)
sxr.serve_forever()

client.py

#coding=utf8
from xmlrpclib import ServerProxy
sp = ServerProxy("http://localhost:8080")
print sp.happy('hi')
print sp.happy('hello')

运行server.py,运行client.py,client可以和server不在同一个机器,调用另一台机器做事情。

原文地址:https://www.cnblogs.com/ydf0509/p/7716844.html