一方面,我让 Django 公开了一个 SOAP 入口点:
肥皂库 1.0
肥皂库处理程序.py
# http://djangosnippets.org/snippets/2210/
from django.http import HttpResponse
import StringIO
from soaplib.serializers.primitive import Boolean, String
from soaplib.service import DefinitionBase, rpc
from soaplib.wsgi import Application
# the class with actual web methods
class MySOAPService(DefinitionBase):
@rpc(String, String, _returns=String)
def foo(self, t1, t2):
return "OK"
# the class which acts as a wrapper between soaplib WSGI functionality and Django
class DjangoSoapApp(Application):
def __call__(self, request):
# wrap the soaplib response into a Django response object
django_response = HttpResponse()
def start_response(status, headers):
status, reason = status.split(' ', 1)
django_response.status_code = int(status)
for header, value in headers:
django_response[header] = value
response = super(DjangoSoapApp, self).__call__(request.META, start_response)
django_response.content = '\n'.join(response)
return django_response
核心视图.py
from soaplib_handler import DjangoSoapApp, MySOAPService
my_soap_service = DjangoSoapApp([MySOAPService], 'Mydeal')
网址.py
url(r'^hello_world/', 'core.views.my_soap_service'),
url(r'^hello_world/service.wsdl', 'core.views.my_soap_service'),
当我做 http://localhost:8001/hello_world/service.wsdl我收到:
<?xml version='1.0' encoding='UTF-8'?>
<wsdl:definitions xmlns:plink="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:senc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s0="Mydeal" xmlns:s12env="http://www.w3.org/2003/05/soap-envelope/" xmlns:s12enc="http://www.w3.org/2003/05/soap-encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="Mydeal" name="DjangoSoapApp">
<wsdl:types>
<xs:schema targetNamespace="Mydeal" elementFormDefault="qualified">
<xs:element name="foo" type="s0:foo" />
<xs:element name="fooResponse" type="s0:fooResponse" />
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="t1" type="xs:string" minOccurs="0" nillable="true" />
<xs:element name="t2" type="xs:string" minOccurs="0" nillable="true" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="fooResponse">
<xs:sequence>
<xs:element name="fooResult" type="xs:string" minOccurs="0" nillable="true" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="foo">
<wsdl:part name="foo" element="s0:foo" />
</wsdl:message>
<wsdl:message name="fooResponse">
<wsdl:part name="fooResponse" element="s0:fooResponse" />
</wsdl:message>
<wsdl:service name="DjangoSoapApp">
<wsdl:port name="DjangoSoapApp" binding="s0:DjangoSoapApp">
<soap:address location="http://localhost:8001/hello_world/service" />
</wsdl:port>
</wsdl:service>
<wsdl:portType name="DjangoSoapApp">
<wsdl:operation name="foo" parameterOrder="foo">
<wsdl:input name="foo" message="s0:foo" />
<wsdl:output name="fooResponse" message="s0:fooResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="DjangoSoapApp" type="s0:DjangoSoapApp">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="foo">
<soap:operation soapAction="foo" style="document" />
<wsdl:input name="foo">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="fooResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
</wsdl:definitions>
目前看起来还可以
然后测试时间
from suds.client import Client
WSDL = "http://localhost:8001/hello_world/service.wsdl"
client_endpoint = Client(WSDL)
print client_endpoint
给出以下错误:
======================================================================
ERROR: test_callback (__main__.TestM2TScenarioI)
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests.py", line 370, in test_callback
client_endpoint = Client(WSDL)
File "/home/gregory/.virtualenvs/casadeal/lib/python2.6/site-packages/suds/client.py", line 112, in __init__
self.wsdl = reader.open(url)
File "/home/gregory/.virtualenvs/casadeal/lib/python2.6/site-packages/suds/reader.py", line 152, in open
d = self.fn(url, self.options)
File "/home/gregory/.virtualenvs/casadeal/lib/python2.6/site-packages/suds/wsdl.py", line 158, in __init__
self.resolve()
File "/home/gregory/.virtualenvs/casadeal/lib/python2.6/site-packages/suds/wsdl.py", line 207, in resolve
c.resolve(self)
File "/home/gregory/.virtualenvs/casadeal/lib/python2.6/site-packages/suds/wsdl.py", line 495, in resolve
raise Exception("msg '%s', not-found" % op.input)
Exception: msg 's0:Test', not-found
----------------------------------------------------------------------
Ran 1 test in 0.053s