Skip to content

Instantly share code, notes, and snippets.

@tmcolby
Created August 13, 2021 19:35
Show Gist options
  • Save tmcolby/41924f9fa4cdc19f9a6647767b687ba4 to your computer and use it in GitHub Desktop.
Save tmcolby/41924f9fa4cdc19f9a6647767b687ba4 to your computer and use it in GitHub Desktop.
simple web server that allows a host to post a file transfer
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