Get“Method Not Allowed”调用Django SOAP web时出错

2024-04-20 01:04:30 发布

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

我用this tutorial编写了一个带有Django的soapweb服务器;我使用了本教程及其链接教程中讨论的代码。在

这是我的urls.py顺便说一句:

from django.conf.urls import url
from django.contrib import admin
from views import hello_world_service

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^hello_world/', hello_world_service),
    url(r'^hello_world/service.wsdl', hello_world_service),
]

为了调用方法“say chu hello”,我编写了多个使用不同库的客户端:

使用suds实现:

^{pr2}$

这是客户端的错误回溯:

Traceback(most recent call last):
    File "client.py", line 14, in < module > client = SudsClient(url=url, cache=None)
    File "build/bdist.linux-x86_64/egg/suds/client.py", line 112, in __init__
    File "build/bdist.linux-x86_64/egg/suds/reader.py", line 152, in open
    File "build/bdist.linux-x86_64/egg/suds/wsdl.py", line 136, in __init__
    File "build/bdist.linux-x86_64/egg/suds/reader.py", line 79, in open
    File "build/bdist.linux-x86_64/egg/suds/reader.py", line 95, in download
    File "build/bdist.linux-x86_64/egg/suds/transport/https.py", line 60, in open
    File "build/bdist.linux-x86_64/egg/suds/transport/http.py", line 64, in open
suds.transport.TransportError: HTTP Error 405: Method Not Allowed

这是我在服务器控制台中看到的:

[24/Jul/2017 08:17:14] "GET /hello_world HTTP/1.1" 301 0
[24/Jul/2017 08:17:14] "GET /hello_world/ HTTP/1.1" 405 0

另一个使用suds的实现:

我找到了here。在

from Tkinter import *
from suds.client import *
class SoapClass:
    def __init__(self, master):
        self.client = Client('http://127.0.0.1:5000/hello_world/', username='', password='', faults=False)
        Button(master, text='Call', command=self.request).pack()
    def request(self):
        methodName = 'getSmsDeliveryStatus'
        params = ['2656565656565']
        MethodToExecute = getattr(self.client.service, methodName)
        try:
            response = MethodToExecute(*params)
        except WebFault as e:
            response = e
        print(response)
root = Tk()
app = SoapClass(root)
root.mainloop()

此客户机在客户端返回与suds实现完全相同的stacktrace;但是服务器控制台显示一个不同的错误:

[24/Jul/2017 08:30:27] "GET /hello_world/ HTTP/1.1" 405 0

使用requests实现:

import requests
target_url = "http://127.0.0.1:5000/hello_world/"
headers = {'Content-type': 'text/xml'}
data = {'id': '322424234234'}
print requests.post(target_url, data=data, headers=headers).text

在客户端,我得到一个HTML页面,该页面通常显示CSRF验证失败。请求中止。在服务器端,我得到:

Forbidden (CSRF cookie not set.): /hello_world/
[24/Jul/2017 08:44:51] "POST /hello_world/ HTTP/1.1" 403 2857

编辑1:

我尝试为这个解决方案禁用CSRF;我得到了:

<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:plnk="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<SOAP-ENV:Body><SOAP-ENV:Fault>
<faultcode>Server</faultcode>
<faultstring>'NoneType' object has no attribute 'startswith'</faultstring>
<detail>Traceback (most recent call last):
    File "/usr/lib/python2.7/dist-packages/soaplib/wsgi_soap.py", line 224, in __call__
        if methodname.startswith('"') and methodname.endswith('"'):
            AttributeError: 'NoneType' object has no attribute 'startswith'
</detail>
</SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

我想我忘了在请求中填写一些参数,有人能帮我吗?在

我的客户机从服务器获取连接并调用方法有什么问题?我必须在服务器/客户端设置一些选项吗?在


Tags: inpybuildhttpurlhelloworldegg