【问题标题】:How can I create a basic Soap server with spyne in django如何在 django 中使用 spyne 创建一个基本的 Soap 服务器
【发布时间】:2022-08-16 11:12:28
【问题描述】:

我想创建一个使用 Django 的服务器,并将响应作为 SOAP 进行响应,因此出于这个原因我尝试使用 spyne,但我无法运行给定的代码

class HelloWorldService(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(ctx, name, times):
        \"\"\"Docstrings for service methods appear as documentation in the wsdl.
        <b>What fun!</b>
        @param name the name to say hello to
        @param times the number of times to say hello
        @return the completed array
        \"\"\"

        for i in range(times):
            yield u\'Hello, %s\' % name


application = Application([HelloWorldService], \'spyne.examples.hello.soap\',
                          in_protocol=Soap11(validator=\'lxml\'),
                          out_protocol=Soap11())

wsgi_application = WsgiApplication(application)


if __name__ == \'__main__\':
    import logging

    from wsgiref.simple_server import make_server

    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger(\'spyne.protocol.xml\').setLevel(logging.DEBUG)

    logging.info(\"listening to http://127.0.0.1:8000\")
    logging.info(\"wsdl is at: http://localhost:8000/?wsdl\")

    server = make_server(\'127.0.0.1\', 8000, wsgi_application)
    server.serve_forever()

    标签: django soap spyne


    【解决方案1】:

    删除 in_protocol 中的 validator='lxml' 选项

    不知道是什么原因,验证器响应时间长或者没有工作。

    并添加导入语句

    from spyne import Application, rpc, ServiceBase, Iterable, Integer, Unicode
    
    from spyne.protocol.soap import Soap11
    from spyne.server.wsgi import WsgiApplication
    
    class HelloWorldService(ServiceBase):
        @rpc(Unicode, Integer, _returns=Iterable(Unicode))
        def say_hello(ctx, name, times):
            """Docstrings for service methods appear as documentation in the wsdl.
            <b>What fun!</b>
            @param name the name to say hello to
            @param times the number of times to say hello
            @return the completed array
            """
    
            for i in range(times):
                yield u'Hello, %s' % name
    
    
    application = Application([HelloWorldService], 'spyne.examples.hello.soap',
                    in_protocol=Soap11(),
                    out_protocol=Soap11())
    
    wsgi_application = WsgiApplication(application)
    
    if __name__ == '__main__':
        import logging
    
        from wsgiref.simple_server import make_server
    
        logging.basicConfig(level=logging.DEBUG)
        logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)
    
        logging.info("listening to http://127.0.0.1:8000")
        logging.info("wsdl is at: http://localhost:8000/?wsdl")
    
        server = make_server('127.0.0.1', 8000, wsgi_application)
        server.serve_forever()
    

    这是我的 Postman 和 curl 测试结果

    POST表扬正文

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <say_hello xmlns="spyne.examples.hello.soap">
            <name>Tom Cruise</name>
            <times>3</times>
        </say_hello>
      </soap:Body>
    </soap:Envelope>
    

    用于终端管道的带有 XMLlint 的 curl POST 命令

    $ curl --location --request POST 'http://localhost:8000' --header 'Content-Type: text/xml; charset=utf-8' --header 'SOAPAction: say_hello' --data-raw '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <say_hello xmlns="spyne.examples.hello.soap">
            <name>Tom Cruise</name>
            <times>3</times>
        </say_hello>
      </soap:Body>
    </soap:Envelope>
    ' | xmllint --format -
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   707  100   427  100   280   9319   6111 --:--:-- --:--:-- --:--:-- 15711
    <?xml version="1.0" encoding="UTF-8"?>
    <soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="spyne.examples.hello.soap">
      <soap11env:Body>
        <tns:say_helloResponse>
          <tns:say_helloResult>
            <tns:string>Hello, Tom Cruise</tns:string>
            <tns:string>Hello, Tom Cruise</tns:string>
            <tns:string>Hello, Tom Cruise</tns:string>
          </tns:say_helloResult>
        </tns:say_helloResponse>
      </soap11env:Body>
    </soap11env:Envelope>
    

    还有我的python版本和spyne版本

    $ python --version
    Python 3.10.4
    
    $ pip show spyne
    Name: spyne
    Version: 2.14.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多