哪个简单的基于python的WSGI兼容jsonrpc库在服务器端用于“pyjamas”?

2024-05-23 17:54:26 发布

您现在位置:Python中文网/ 问答频道 /正文

最近,我遇到了pyjamas框架。它鼓励完全不同的web应用程序开发方法,它将MVC的整个“视图”组件分离成一些html+javascript(用编译的python生成),而不是使用传统的模板。这个客户端“视图”应该通过异步HTTP请求与服务器通信,框架建议使用“jsonrpc”作为通信协议。在

在他们的文档中,他们使用了一个基于django的jsonrpc组件。但我大多习惯于简单而愚蠢的解决方案,比如bottle framework。据我所知,我甚至不需要这些微框架的所有组件。一个WSGI兼容的服务器、一些路由+会话中间件和一个能够理解jsonrpc的请求处理程序就可以了。我正在为最后一部分寻找一个易于使用的轻量级解决方案-随时可用的jsonrpc感知请求处理程序,它可以很好地插入WSGI环境中。他们有吗?在

请原谅并纠正我对术语的误用/误解。在


Tags: 方法服务器框架视图web处理程序wsgihtml
2条回答

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]

你现在可能已经选择了一些图书馆。但无论如何。在

我用烧瓶和jsonrpc2。这是一些psudo代码。我的代码非常相似。在

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)

相关问题 更多 >