我正在使用 wave.open 打開檔案,如果我提供本地路徑
async def run_test(uri):
async with websockets.connect(uri) as websocket:
wf = wave.open('test.wav', "rb")
那么它正在作業,但如果我給外部路徑它不起作用
async def run_test(uri):
async with websockets.connect(uri) as websocket:
wf = wave.open('http://localhost:8000/storage/uploads/test.wav', "rb")
收到此錯誤:
OSError: [Errno 22] 無效引數: 'http://localhost:8000/storage/uploads/test.wav'
uj5u.com熱心網友回復:
是的,wave.open()對 HTTP 一無所知。
您需要先下載檔案,例如使用requests(aiohttp或httpx因為您處于異步狀態)。
import io, requests, wave
resp = requests.get('http://localhost:8000/storage/uploads/test.wav')
resp.raise_for_status()
bio = io.BytesIO() # file-like memory buffer
bio.write(resp.content) # todo: if file can be large, maybe use streaming
bio.seek(0) # seek to the start of the file
wf = wave.open(bio, "rb") # wave.open accepts file-like objects
這假設檔案足夠小以適合記憶體;如果不是,你會想用它tempfile.NamedTemporaryFile來代替。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/384558.html
