Skip to content

Instantly share code, notes, and snippets.

@cyberbutler
Last active March 22, 2022 15:59
Show Gist options
  • Select an option

  • Save cyberbutler/ff9055daf787f39daf4cf58af9045e24 to your computer and use it in GitHub Desktop.

Select an option

Save cyberbutler/ff9055daf787f39daf4cf58af9045e24 to your computer and use it in GitHub Desktop.
A basic Flask web server for uploading files
import os
import datetime
from werkzeug.utils import secure_filename
from flask import Flask, request, jsonify
app = Flask(__name__)
app.config['UPLOAD_DIR'] = 'uploads'
@app.route('/', methods=['POST'])
def upload_file():
# Loop through each file in the request and save to disk with
# the name and timestamp of extraction
for parameter, f in request.files.items():
currentTimeStamp = datetime.datetime.now().strftime('%d-%b-%Y-%H-%M-%S')
# Create the filename by concatenating the currentTimeStame and POSTed filename
fileName = secure_filename(f"{currentTimeStamp}.{f.filename}")
# Create the upload path by concatenating the
# specified upload directory and the Remote Address
fileDir = os.path.join(app.config['UPLOAD_DIR'], request.remote_addr)
# Create the fileDir directory if it doesn't exist
if not os.path.exists(fileDir):
os.makedirs(fileDir)
# Save the file to the fileDir
f.save(os.path.join(fileDir, fileName))
return jsonify({"status": "success"})
if __name__ == "__main__":
# Run the server in Debug mode on socket 0.0.0.0:80
app.run('0.0.0.0', port=80, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment