带有Django的XML-RPC服务器API

问题描述

我正在尝试使用XML-RPC协议使用Django创建API。我实际上使用Python创建了一些基本示例,但是在Django上实现这些示例时,我开始遇到很多问题。

我正在使用this代码,我认为它已经很老了,但是找不到更好的教程。

我的urls.py

urlpatterns = [
    path('xml_rpc',views.rpc_handler),]

这是我的Django views.py

from xmlrpc.server import SimpleXMLRPCDispatcher
from django.http import HttpResponse

# Create a Dispatcher; this handles the calls and translates info to function maps
#dispatcher = SimpleXMLRPCDispatcher() # Python 2.4
dispatcher = SimpleXMLRPCDispatcher() # Python 2.5


@csrf_exempt
def rpc_handler(request):
        if len(request.POST):
            response = HttpResponse(mimetype="application/xml")
            response.write(dispatcher._marshaled_dispatch(request.raw_post_data))
        else:
            response = HttpResponse()
            response.write("<b>This is an XML-RPC Service.</b><br>")
            response.write("You need to invoke it using an XML-RPC Client!<br>")
            response.write("The following methods are available:<ul>")
            methods = dispatcher.system_listMethods()

            for method in methods:
                    sig = dispatcher.system_methodSignature(method)

                    help =  dispatcher.system_methodHelp(method)

                    response.write("<li><b>%s</b>: [%s] %s" % (method,sig,help))

            response.write("</ul>")
            response.write('<a href="http://www.djangoproject.com/"> <img src="http://media.djangoproject.com/img/badges/djangomade124x25_grey.gif" border="0" alt="Made with Django." title="Made with Django."></a>')

    response['Content-length'] = str(len(response.content))
    return response

def multiply(a,b):
        return a*b

dispatcher.register_function(multiply,'multiply')

这段代码实际上是有效的,我的意思是,如果我运行服务器,则可以访问该视图,并且可以使用以下代码从Python发送API请求:

import xmlrpc.client

rpc_srv = xmlrpc.client.ServerProxy("http://localhost:8000/xml_rpc")
rpc_srv.multiply(2,2)

但是我得到了这个错误:

Traceback (most recent call last):
File "test.py",line 5,in <module>
rpc_srv.multiply(2,2)
File "/usr/lib/python3.7/xmlrpc/client.py",line 1112,in __call__
return self.__send(self.__name,args)
File "/usr/lib/python3.7/xmlrpc/client.py",line 1452,in __request
verbose=self.__verbose
File "/usr/lib/python3.7/xmlrpc/client.py",line 1154,in request
return self.single_request(host,handler,request_body,verbose)
File "/usr/lib/python3.7/xmlrpc/client.py",line 1170,in single_request
return self.parse_response(resp)
File "/usr/lib/python3.7/xmlrpc/client.py",line 1336,in parse_response
p.feed(data)
File "/usr/lib/python3.7/xmlrpc/client.py",line 439,in feed
self._parser.Parse(data,0)
xml.parsers.expat.ExpatError: junk after document element: line 1,column 34

我知道问题出在服务器上,可以通过在'<root></root>'标记之间添加XML响应来解决,但是由于在Django中代码的实现方式,我什至不知道在哪里放置他们。顺便说一下,该实现是在Python2中进行的,因此我无法在当前XML-RPC Docs中找到有关SimpleXMLRPCDispatcher及其工作方式的任何信息。关于如何使用Django解决此问题的任何解释都很棒。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...