📅  最后修改于: 2023-12-03 15:33:57.591000             🧑  作者: Mango
Python RPC JSON服务器是一种基于JSON格式传输数据的远程过程调用(RPC)技术,可用于在不同的应用程序之间进行通信。
下面是使用Python RPC JSON服务器的基本步骤:
pip install jsonrpcserver
from jsonrpcserver import methods
@methods.add
def add(a, b):
return a + b
if __name__ == '__main__':
from jsonrpcserver import serve
serve()
运行上述代码后,即可在 localhost:5000
上启动一个JSON-RPC服务器。
import requests
response = requests.post('http://localhost:5000', json={
'jsonrpc': '2.0',
'method': 'add',
'params': {'a': 3, 'b': 4},
'id': 0,
})
if response.ok:
print(response.json())
else:
print("An error occurred: %s" % response.text)
运行上述代码后,即可在终端中看到输出结果:{'jsonrpc': '2.0', 'result': 7, 'id': 0}
。
以上就是Python RPC JSON服务器的基本介绍,希望对你有所帮助。