Created
August 13, 2021 19:35
-
-
Save tmcolby/41924f9fa4cdc19f9a6647767b687ba4 to your computer and use it in GitHub Desktop.
simple web server that allows a host to post a file transfer
This file contains 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
from flask import Flask, abort, request | |
from werkzeug.utils import secure_filename | |
import os | |
import requests | |
import json | |
# change this to set where you want the file to be saved | |
UPLOAD_FOLDER = "./" | |
# change this to only allow files with a given extension to be accepted | |
ALLOWED_EXTENSIONS = ["txt", "zip"] | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
# function that returns True if the file that was sent from host has a valid extension | |
def allowed_file(filename): | |
return '.' in filename and filename.split('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
# this handles the route | |
@app.route("/recipe", methods=['POST']) | |
def recipe(store_id=None): | |
# if the client tries to do anything other than POST, abort. | |
if request.method == 'POST': | |
if 'file' not in request.files: | |
abort(404) | |
file = request.files['file'] | |
if file and allowed_file(file.filename): | |
filename = secure_filename(file.filename) #secure_filename just makes sure the file name is valid | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
return {"msg": "file upload success"}, 200 | |
else: | |
abort(404) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0',port=5000, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment