Created
December 14, 2024 08:03
-
-
Save mrrootsec/183bd53405c92e0e7f7d8e74be2728ae to your computer and use it in GitHub Desktop.
Simple utility to share files using flask 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
from flask import Flask, request, jsonify | |
import os | |
app = Flask(__name__) | |
# Directory where uploaded files will be stored | |
UPLOAD_FOLDER = './uploads' | |
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
@app.route('/upload', methods=['POST']) | |
def upload_file(): | |
# Check if the request contains the file part | |
if 'file' not in request.files: | |
return jsonify({'error': 'No file part'}), 400 | |
file = request.files['file'] | |
# If no file is selected | |
if file.filename == '': | |
return jsonify({'error': 'No selected file'}), 400 | |
# Save the file | |
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) | |
file.save(filepath) | |
return jsonify({'message': f'File uploaded successfully! Saved to {filepath}'}), 200 | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment