我正在嘗試創建一項服務以將一些檔案從服務器流式傳輸到客戶端。但是,而不是這樣的 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()
這適用于 URL 之類/get_file?path=file_name的,但對于 URL 之類的/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>
我無法改變客戶這樣要求的事實。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
這可以通過HttpPattern 實作。
首先看看這個:
https://github.com/arskom/spyne/blob/35fa93f1ac34f868b9d86c639b6e7687c1842115/examples/multiple_protocols/server.py
運行守護程式后,轉到:
- http://127.0.0.1:9910/soap/get_utc_time
- http://127.0.0.1:9910/json/get_utc_time
或者:
- http://127.0.0.1:9910/dyn/get_utc_time.json
- http://127.0.0.1:9910/dyn/get_utc_time.soap
- http://127.0.0.1:9910/dyn/get_utc_time.svg
等等
同樣,您的代碼應如下所示:
class FileService(Service):
@rpc(String, _returns=ByteArray,
_patterns=[HttpPattern('/get_file/<file_name>')])
def get_file(ctx, file_name):
print(file_name)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459343.html
