Python JSON-RPC 2.0 API

问题描述 投票:1回答:1

我正在使用json-rpc 1.10.3 lib在Python上创建JSON-RPC API。

我已经制作了服务器和客户端。

在服务器上,我已经完成了两个方法连接到MySQl数据库并检索我需要的字典并将它们添加到调度程序中。

但是当我运行客户端时,我得到了响应405(方法不允许)。

有什么问题?

服务器

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)

客户

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()
python api json-rpc
1个回答
0
投票

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

'http://localhost:80/jsonrpc'
© www.soinside.com 2019 - 2024. All rights reserved.