pip install django-json-rpc
from jsonrpc import jsonrpc_method
@jsonrpc_method('common.persons')
def persons(request):
return [1, 2, 3]
@jsonrpc_method('req')
def person_s(request, *params):
# arr = []
# for i in params:
# arr.append(i)
# print(params)
return list(params)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('common/', include('common.urls')),
]
相对应的模块下的urls.py文件修改如下from django.urls import path
from . import views
from jsonrpc import jsonrpc_site
urlpatterns = [
# path('rpc/', views),
# path('person/', views.person),
path('rpc/', jsonrpc_site.dispatch)
]
http://127.0.0.1:8000/common/rpc/
{
"id": "425d9d70-ccbe-11ea-a8ca-7470fd0bd93a",
"method":"comon.persons",
"params":[1,2,3],
"jsonrpc": "2.0"
}
{
"id": "425d9d70-ccbe-11ea-a8ca-7470fd0bd93a",
"result": [
1,
2,
3
],
"jsonrpc": "2.0"
}
from jsonrpc.proxy import ServiceProxy
url = "http://127.0.0.1:8000/rpc-person/"
s = ServiceProxy(url)
res = s.comon.persons.data()
print(res) # 返回结果同上