Skip to main content
 首页 » 编程设计

python之用Python的spyne修改soap webservice中的WSDL

2025年05月04日20JeffreyZhao

我正在使用 Django 和 Python 进行开发,我需要使用 SOAP 发布 1 个服务和 2 个操作。对于这个任务,我选择了 spyne 库:

http://spyne.io/#auxproc=Sync&s=aux

因为显然很容易理解并开始开发。我做了第一个例子并且很好,甚至我用我的内部逻辑开发了我自己的方法。现在我需要开发其他最特别的功能。我当前的代码是:

class SopoSoap(ComplexModel): 
    __namespace__ = 'http://service/service.wsdl' 
    _type_info = { 
        "field1": Integer(min_occurs = 1), 
        "field2": Integer(min_occurs = 1), 
        "field3": Unicode(min_occurs = 1), 
        "field4": Unicode(min_occurs = 1), 
        "field5": Unicode(min_occurs = 0), 
    } 
 
class NewIncidenceService(ServiceBase): 
    @rpc(SopoSoap, _returns=Integer) 
    def NewIncidence(sop): 
        # my differetns operations in code 
        return a.pk # integer value 
 
application = Application([NewIncidenceService], 
    tns=http://service/service.wsdl', 
    in_protocol=Soap11(validator='lxml'), 
    out_protocol=Soap11(cleanup_namespaces=True) 
) 
 
createSop_app = csrf_exempt(DjangoApplication(application)) 

有了这段代码,一切都很好,我生成的 wsdl 就是这个:

<wsdl:definitions xmlns:wsa="http://schemas.xmlsoap.org/ws/2003/03/addressing" xmlns:tns="http://localhost/DEMAT/DEMAT_IncidenceManagement/service.wsdl" xmlns:plink="http://schemas.xmlsoap.org/ws/2003/05/partner-link/" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:soap12env="http://www.w3.org/2003/05/soap-envelope/" xmlns:senc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap12enc="http://www.w3.org/2003/05/soap-encoding/" targetNamespace="http://localhost/DEMAT/DEMAT_IncidenceManagement/service.wsdl" name="Application">...</wsdl:definitions> 

现在我需要这些改变:

首先,如果我尝试这个来源:

class ResponseData(ComplexModel): 
    message = Unicode 
    codResultado = Integer 
 
 
class CreateIncidenceService(ServiceBase): 
    @rpc(SopoSoap, _returns=ResponseData) 
    def NewIncidence(sop): 
        try: 
            # operations 
            return ResponseData 

我从来没有收到我的服务器的答复(Apache with django - wsgi.py),我需要更改 rpc 装饰器吗?返回的类型,我在哪里可以找到一个很好的傻瓜文档示例?

其次。这对我来说非常重要,我需要更改 wsdl 中特定元素的名称,例如:

<xs:complexType name="NewIncidenceResponse"> 
or this others: 
<wsdl:message name="NewIncidence"> 
<wsdl:part name="NewIncidence" element="tns:NewIncidence"/> 
</wsdl:message> 
<wsdl:message name="NewIncidenceResponse"> 
<wsdl:part name="NewIncidenceResponse" element="tns:NewIncidenceResponse"/> 
</wsdl:message> 

名称,只有名称,我想这应该很简单,因为在 Java 或 .net 中,您可以毫无问题地更改这些参数的名称,但是有了这个库,我不知道我该怎么做?

三,我想返回一个具有 3 个字段的结构的 complexType:

一)代码 b) 讯息 c) Exception:这里不知道怎么把异常返回给wsdl。

对于这 3 个字段,我想在我创建的 responseData 类中,但我无法返回此类数据。我知道我问了 3 个问题,但我正在阅读 spyne 的所有文档,但没有找到任何问题。

请您参考如下方法:

首先,感谢您试用 Spyne!

答案:

  1. 尝试

    return ResponseData(codResultado=1234, message="Hello!") 
    
  2. 将 _out_response_name 传递给 @rpc

  3. 不要自己发明并使用内置的 Fault 类。

Spyne 文档很糟糕,是的,但它们并没有那么糟糕。阅读它们:)

Spyne 还有一个邮件列表:http://lists.spyne.io/listinfo/people

嗯,