Created
November 15, 2022 15:24
-
-
Save marcs-feh/b4ea7c236166490dc6a35d09c6c41275 to your computer and use it in GitHub Desktop.
Quickly serve a file using flask, I use this frequently to transfer files to a VM using port fowarding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from sys import argv, exit | |
from flask import Flask | |
from os.path import exists | |
PORT=8069 | |
def main(): | |
if len(argv) < 2: | |
print('provide a file to be served over HTTP') | |
exit(-1) | |
file_path = argv[1] | |
if exists(file_path): | |
app = Flask(__name__) | |
app.static_folder = '.' | |
print(f'Serving "{file_path}" on port {PORT}') | |
index_handler = lambda : app.send_static_file(file_path) | |
app.add_url_rule(rule='/', view_func=index_handler) | |
app.run(port=PORT, debug=False) | |
else: | |
print(f"no such file or directory '{file_path}'") | |
if __name__ == '__main__': main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment