-
-
Save abdulazizalmass/0d39f9e7060bd5e00a01691820f37fb6 to your computer and use it in GitHub Desktop.
Bare bones Python (Flask) Upload/Download Server
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
""" | |
Bare bones download/upload Python server using Flask. | |
""" | |
import os | |
from flask import ( | |
Flask, redirect, render_template_string, request, send_from_directory | |
) | |
from werkzeug.utils import secure_filename | |
UPLOAD_FOLDER = '.' | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
@app.route('/<path:filename>') | |
def _downloadFile(filename): | |
return send_from_directory( | |
os.path.abspath('.'), | |
filename, | |
as_attachment=True | |
) | |
@app.route('/', methods=['GET', 'POST']) | |
def uploadFile(): | |
if request.method == 'GET': | |
filenames = os.listdir('.') | |
return render_template_string(HOME_PAGE, files=filenames) | |
if request.method == 'POST': | |
if request.files['file']: | |
file = request.files['file'] | |
filename = secure_filename(file.filename) | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
return redirect(request.url) | |
HOME_PAGE = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Logfiles</title> | |
</head> | |
<h1>Download File</h1> | |
<body> | |
<ul> | |
{% for file in files %} | |
<li><a href="{{ url_for('_downloadFile', filename=file) }}">{{ file }}</a></li> | |
{% endfor %} | |
</ul> | |
</body> | |
<h1>Upload File</h1> | |
<form method=post enctype=multipart/form-data> | |
<input type=file name=file> | |
<input type=submit value=Upload> | |
</form> | |
</html> | |
""" | |
#it is recommended to have zeros in host to catch an ip from DHCP or staticly configuring it then mapping it to in the below host variable | |
#https://stackoverflow.com/questions/7023052/configure-flask-dev-server-to-be-visible-across-the-network | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=8000, debug=True, threaded=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment