You can create an HTTP API using Sanic WebSockets library in Python 3 that allows files to be requested and streamed from a specific directory. Here is the sample code to achieve this:
main.py452 chars16 lines
In this code, a new Sanic app is initialized and a function named download()
is defined with <filename>
as a parameter in the route "/download/". This function takes in a filename parameter, reads the file from the specified directory and returns the file as a response. The response is based on the file's MIME type, which is set to 'application/octet-stream' enabling it to be downloaded like files without defined MIME types.
To test this API use the curl command in your terminal:
73 chars2 lines
This will download the file locally in the directory from which the command was executed.
gistlibby LogSnag