简体   繁体   English

Python JSON-RPC 2.0 API

[英]Python JSON-RPC 2.0 API

I'm creating a JSON-RPC API on Python using json-rpc 1.10.3 lib. 我正在使用json-rpc 1.10.3 lib在Python上创建JSON-RPC API

I've made the server and client. 我已经制作了服务器和客户端。

On the server I've done two methods which connect to MySQl database and retrieve the dictionary I need and added them to the dispatcher. 在服务器上,我已经完成了两个方法连接到MySQl数据库并检索我需要的字典并将它们添加到调度程序中。

But when I run the client, I'm getting the Response 405 (Method Not Allowed) . 但是当我运行客户端时,我得到了响应405(方法不允许)

What's the problem? 有什么问题?

SERVER 服务器

from werkzeug.wrappers import Request, Response
from werkzeug.serving import run_simple
from jsonrpc import JSONRPCResponseManager, dispatcher

HOST = 'localhost'
PORT = 80

def __init__(self, conn):
    self.conn = conn

@dispatcher.add_method
def get_detailed_usage(self, serviceRef, fromDate, toDate, excludeCallsCoveredByBundleAllowance):
    sql_where = []
    sql_vars = []
    if serviceRef is not None:
        sql_where.append("pc.service_ref={}").format(serviceRef)
        sql_vars.append(serviceRef)
    if fromDate is not None:
        sql_where.append("rc.start_time={}").format(fromDate)
        sql_vars.append(fromDate)
    if toDate is not None:
        sql_where.append("rc.end_time={}").format(toDate)
        sql_vars.append(toDate)
    if excludeCallsCoveredByBundleAllowance is not None:
        sql_where.append("excludeCallsCoveredByBundleAllowance={}").format(excludeCallsCoveredByBundleAllowance)
        sql_vars.append(excludeCallsCoveredByBundleAllowance)
    sql_query = """
                SELECT...
                FROM...
                JOIN...
                ON...
                """
    if sql_where:
        sql_query += " WHERE" + " AND ".join(sql_where)
    cursor = self.conn.cursor()
    cursor.execute(sql_query, *sql_vars)
    result = cursor.fetchall()

    return dict(result)


@dispatcher.add_method
def get_summary_usage(self, serviceRef, fromDate, toDate, excludeCallsCoveredByBundleAllowance):
    sql_where = []
    sql_vars = []
    if serviceRef is not None:
        sql_where.append("pc.service_ref={}").format(serviceRef)
        sql_vars.append(serviceRef)
    if fromDate is not None:
        sql_where.append("rc.start_time={}").format(fromDate)
        sql_vars.append(fromDate)
    if toDate is not None:
        sql_where.append("rc.end_time={}").format(toDate)
        sql_vars.append(toDate)
    if excludeCallsCoveredByBundleAllowance is not None:
        sql_where.append("excludeCallsCoveredByBundleAllowance={}").format(excludeCallsCoveredByBundleAllowance)
        sql_vars.append(excludeCallsCoveredByBundleAllowance)
    sql_query = """
                SELECT...
                FROM...
                JOIN...
                ON...
                """
    if sql_where:
        sql_query += " WHERE" + " AND ".join(sql_where)
    cursor = self.conn.cursor()
    cursor.execute(sql_query, *sql_vars)
    result = cursor.fetchall()

    return dict(result)

def application(request):
    response = JSONRPCResponseManager.handle(request.data, dispatcher)
    return Response(response.json, response.http_status, mimetype='application/json')


if __name__ == '__main__':
    run_simple(HOST, PORT, application)

CLIENT 客户

import requests
import json


def main():
    url = 'http://localhost:80'
    # headers = {'content-type': 'application/json'}

    result = {
        "jsonrpc": "2.0",
        "method": "get_detailed_usage",
        "params": [{"serviceRef": "1234", "fromDate": "2017-01-01 00:00:00", "toDate": "2017-01-31 23:59:59"}],
        "id": 0,
    }
    response = requests.post(url, data=json.dumps(result))

    print(response)


if __name__ == "__main__":
    main()

我认为在客户端,正确的URL应该是

'http://localhost:80/jsonrpc'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM