使用XML库的方式,实现RPC通信的方法(推荐)
作者:jingxian 时间:2021-11-15 14:00:42
1、先说结论:使用xml-rpc的机制可以很方便的实现服务器间的RPC调用。
2、试验结果如下:
3、源码如下:
服务器端的源代码如下:
import operator, math
from SimpleXMLRPCServer import SimpleXMLRPCServer
from functools import reduce
def main():
server = SimpleXMLRPCServer(('127.0.0.1', 7001))
server.register_introspection_functions()
server.register_multicall_functions()
server.register_function(addtogether)
server.register_function(quadratic)
server.register_function(remote_repr)
print("Server ready")
server.serve_forever()
def addtogether(*things):
"""Add together everything in the list things ."""
return reduce(operator.add, things)
def quadratic(a, b, c):
"""Determine x values satisfying: a * x * x + b * x + c = 0"""
b24ac = math.sqrt(b*b - 4.0*a*c)
return list(set([(-b-b24ac) / 2.0*a, (-b+b24ac) / 2.0*a]))
def remote_repr(arg):
"""return the repr() rendering of the supplied arg """
return arg
if __name__ == '__main__':
main()
客户端的代码如下:
import xmlrpclib
def main():
proxy = xmlrpclib.ServerProxy('http://127.0.0.1:7001')
print("Here are the functions supported by this server:")
print("next calculator addtogether: ")
print(proxy.addtogether('x','y','z'))
print(proxy.addtogether('x','y','z'))
print(proxy.addtogether('x','y','z'))
print(proxy.addtogether('x','y','z'))
for method_name in proxy.system.listMethods():
if method_name.startswith('system.'):
continue
signatures = proxy.system.methodSignature(method_name)
if isinstance(signatures, list) and signatures:
for signature in signatures:
print('%s(%s)' %(method_name, signature))
else:
print('%s(...)' %(method_name,))
method_help = proxy.system.methodHelp(method_name)
#if method_help:
# print(' ', methodHelp)
print(proxy.addtogether('x','y','z'))
print("addtogether result ")
if __name__ == '__main__':
main()
标签:XML库,RPC通信
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php 进阶:实现无限分类第1/4页
2023-11-17 21:04:55
python求绝对值的三种方法小结
2023-04-17 19:57:09
![](https://img.aspxhome.com/file/2023/0/72820_0s.jpg)
Python使用signal定时结束AsyncIOScheduler任务的问题
2022-12-19 21:28:11
Python实现的寻找前5个默尼森数算法示例
2023-05-15 17:34:29
一个很棒的js图片代码
2009-12-17 18:36:00
![](https://img.aspxhome.com/file/UploadPic/200912/17/texiao-10s.gif)
pandas学习之txt与sql文件的基本操作指南
2022-08-03 18:57:50
![](https://img.aspxhome.com/file/2023/8/86408_0s.png)
科讯cms文章分页列表页码样式的修改
2008-07-10 13:11:00
![](https://img.aspxhome.com/file/UploadPic/20087/10/kesion-cms_17s.jpg)
随机显示图片
2009-07-26 10:13:00
python上下文管理的使用场景实例讲解
2022-01-31 08:38:33
ASP进阶教程Ⅶ:留言簿设置密码验证
2008-10-13 09:02:00
如何调试 XMLHttpRequest
2008-08-15 13:59:00
![](https://img.aspxhome.com/file/UploadPic/20088/15/200881514315131s.gif)
PHPExcel冻结(锁定)表头的简单实现方法
2023-08-18 02:35:21
![](https://img.aspxhome.com/file/2023/9/55379_0s.jpg)
python 爬取豆瓣网页的示例
2021-06-25 22:50:00
![](https://img.aspxhome.com/file/2023/5/86255_0s.png)
解决Scrapy安装错误:Microsoft Visual C++ 14.0 is required...
2023-03-06 19:48:57
Python线程threading模块用法详解
2023-01-30 03:55:01
pycharm部署django项目到云服务器的详细流程
2021-05-27 23:32:19
![](https://img.aspxhome.com/file/2023/4/94064_0s.png)
Python导入数值型Excel数据并生成矩阵操作
2023-05-15 16:59:24
![](https://img.aspxhome.com/file/2023/3/64263_0s.jpg)
python处理cookie详解
2023-09-27 19:14:36
PHP PDOStatement::errorInfo讲解
2023-06-04 14:11:53
python使用plot绘制未来15天气温折线图
2022-11-06 02:09:41
![](https://img.aspxhome.com/file/2023/0/93620_0s.png)