Spyne - GET 具有多个路径而不是查询参数

Spyne - GET with multiple paths instead of parameters for the query

我正在尝试创建一项服务以将一些文件从服务器流式传输到客户端。但是,而不是像这样的 URL:

$ curl http://localhost:8000/get_file?path=file_name

客户端这样请求文件:

$ curl http://localhost:8000/get_file/file_name

这在 Spyne 中可行吗?

看着file_soap_http,服务器我是这样写的:

# server.py
from spyne import (
    Service, rpc, Application,
    String,
    ByteArray,
)
from spyne.protocol.http import HttpRpc
from spyne.protocol.soap import Soap11
from spyne.server.wsgi import WsgiApplication
from wsgiref.simple_server import make_server

class FileService(Service):
    @rpc(String, _returns=ByteArray)
    def get_file(ctx, file_name):
        print(file_name)


application = Application(
    [FileService],
    'FileService',
    in_protocol=HttpRpc(),
    out_protocol=Soap11(),
)

wsgi_application = WsgiApplication(application)

if __name__ == '__main__':
    server = make_server('0.0.0.0', 8000, wsgi_application)
    server.serve_forever()

这适用于 URLs,如 /get_file?path=file_name,但对于 URLs,如 /get_file/file_name,它给出 Requested resource not found 错误:

<soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap11env:Body>
    <soap11env:Fault>
      <faultcode>soap11env:Client.ResourceNotFound</faultcode>
      <faultstring>Requested resource '{FileService}file_name' not found</faultstring>
      <faultactor></faultactor>
    </soap11env:Fault>
  </soap11env:Body>
</soap11env:Envelope>

我无法改变客户这样要求的事实。我怎样才能做到这一点?

这可以通过 HttpPattern 实现。

先看看这个:

https://github.com/arskom/spyne/blob/35fa93f1ac34f868b9d86c639b6e7687c1842115/examples/multiple_protocols/server.py

一旦你运行守护进程,去:

或者:

等等

同样,您的代码应如下所示:

class FileService(Service):
    @rpc(String, _returns=ByteArray,
        _patterns=[HttpPattern('/get_file/<file_name>')])
    def get_file(ctx, file_name):
        print(file_name)