后台小程序开发的全方位指南
1563
2022-10-13
python-jrpc-一个基于JSON RPC v2.0 的python 远程调用框架
Python-JRPC
A Python remote procedure call framework that uses JSON RPC v2.0
Install using pip:
pip install python-jrpc
Socket Based Usage
Python-JRPC allows programmers to create powerful client/server programs with very little code. Here's an example of a server and client:
Server
import jrpcclass SimpleService(jrpc.service.SocketObject): @jrpc.service.method def echo(self, msg): return msgserver = SimpleService(50001) #Include the listening portserver.run_wait()
Client
import jrpcserver = Noneserver = jrpc.service.SocketProxy(50001) #The server's listening portprint server.echo("Hello World!")
Web Based (Flask) Usage
A recent addition to Python JRPC offers Flask integration. Using this feature, client side Javascript can easily call webservice API methods.
Flask App
from jrpc.web import *import jrpc.servicefrom flask import Flask, render_templateapp = Flask(__name__)class WebService(JRPCBlueprint): def __init__(self): JRPCBlueprint.__init__(self, "service", __name__, url_prefix="/api") @jrpc.service.method(path = "/echo/
Index.html Javascript
jrpc("/api").done(function(api) { api.echo({name: "Python JRPC", text: "Now with more Flask"}).done(function(result) { $("#result").text(JSON.stringify(result)); });});
Result
{"subject":"Hello from Python JRPC", "message":"Now with more Flask"}
Why Use It?
Python-JRPC takes all of the boiler-plate code out of your network applications. Forget reading through socket documentation and developing your own message formats. All you need to do is write your Python server/client logic and let Python-JRPC handle the networking for you. Here's what you get:
Remote method call with JSON serializable parameters/return valuesSynchronization/thread safety in servers/clientsRemote exception passing (When calling a remote method in a client, exceptions thrown by the server code will be thrown locally!)Simplified Flask applications, client side Javascript can easily call webservice API methods
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~