Recently, I came across pyjamas framework. It encourages radically different web application development approach by seperating the whole 'view' component of 'MVC' into some html + javascript (generated with compiled python), instead of using traditional templating. This client side 'view' is supposed to communicate with the server through Asynchronous HTTP Requests, and the framework recommends using 'jsonrpc' as communication protocol.
In their documentation, they used a django based jsonrpc component. But I am mostly used to simple and stupid solutions like bottle framework. As far as I understand, I don't even need all the components of such microframeworks. A WSGI compatible server, some routing + session middleware and a request handler which understands in terms of jsonrpc will do just fine. I am looking for an easy to use light weight solution for the last part - readily available jsonrpc-aware request handler that plugs nicely in WSGI environment. Is their any?
Please pardon and correct my misuse/misunderstanding of terms, if any.
You might have chosen some library by now. But anyways.
I use flask and and jsonrpc2. Here is some psudo code. My code is very similar.
import jsonrpc2
mapper = jsonrpc2.JsonRpc()
mapper['echo'] = str
@app.route('/rpc', methods=['GET', 'POST'])
def rpc():
#req {"jsonrpc": "2.0", "method": methodname, "params": params, "id": 1}
data = mapper(request.json)
return jsonify(data)
https://github.com/dengzhp/simple-jsonrpc
import jsonrpc
def add(a, b):
return a + b
def default(*arg, **kwargs):
return "hello jsonrpc"
class MyJsonrpcHandler(jsonrpc.JsonrpcHandler):
"""define your own dispatcher here"""
def dispatch(self, method_name):
if method_name == "add":
return add
else:
return default
def application(environ, start_response):
# assert environ["REQUEST_METHOD"] = "POST"
content_length = int(environ["CONTENT_LENGTH"])
# create a handler
h = MyJsonrpcHandler()
# fetch the request body
request = environ["wsgi.input"].read(content_length)
# pass the request body to handle() method
result = h.handle(request)
#log
environ["wsgi.errors"].write("request: '%s' | response: '%s'\n" % (request, result))
start_response("200 OK", [])
return [result]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With