Skip to content

Instantly share code, notes, and snippets.

@adrianlzt
Created November 25, 2025 09:50
Show Gist options
  • Select an option

  • Save adrianlzt/d23432808615f31e545b761309e5bc06 to your computer and use it in GitHub Desktop.

Select an option

Save adrianlzt/d23432808615f31e545b761309e5bc06 to your computer and use it in GitHub Desktop.

File Upload Server

A simple Python Flask API for uploading files via curl or other HTTP clients.

Features

  • Simple API endpoint for file uploads
  • Stores files locally in uploads/ directory
  • JSON responses
  • Secure filename handling
  • 100MB max file size
  • Uses uv inline dependencies (no separate requirements.txt needed)

Usage

Start the server

chmod +x app.py
./app.py

The server will start on http://localhost:5000

Upload a file with curl

# Upload a file
curl -F 'file=@/path/to/your/file.txt' http://localhost:5000/upload

# Upload with verbose output
curl -v -F 'file=@document.pdf' http://localhost:5000/upload

# Upload and save response to a file
curl -F 'file=@image.png' http://localhost:5000/upload -o response.json

Response format

Success (201):

{
  "message": "File uploaded successfully",
  "filename": "file.txt",
  "path": "uploads/file.txt",
  "size": 1024
}

Error (400):

{
  "error": "No file part in the request"
}

Health check

curl http://localhost:5000/health

Configuration

Edit these variables in app.py:

  • UPLOAD_FOLDER: Directory where files are stored (default: 'uploads')
  • MAX_CONTENT_LENGTH: Maximum file size in bytes (default: 100MB)

Requirements

#!/usr/bin/env -S uv run
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "flask>=3.0.0",
# ]
# [tool.uv]
# exclude-newer = "2025-06-03T00:00:00Z"
# ///
from flask import Flask, request, jsonify
import os
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB max file size
# Create uploads directory if it doesn't exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
@app.route('/upload', methods=['POST'])
def upload_file():
"""Handle file upload via API"""
# Check if a file was submitted
if 'file' not in request.files:
return jsonify({'error': 'No file part in the request'}), 400
file = request.files['file']
# Check if user selected a file
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
# Save the file
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
return jsonify({
'message': 'File uploaded successfully',
'filename': filename,
'path': filepath,
'size': os.path.getsize(filepath)
}), 201
@app.route('/health', methods=['GET'])
def health():
"""Health check endpoint"""
return jsonify({'status': 'ok'}), 200
if __name__ == '__main__':
print(f"File upload server starting...")
print(f"Upload directory: {os.path.abspath(app.config['UPLOAD_FOLDER'])}")
print(f"Max file size: {app.config['MAX_CONTENT_LENGTH'] / 1024 / 1024:.0f}MB")
print(f"\nExample usage:")
print(f" curl -F 'file=@/path/to/file.txt' http://localhost:5000/upload")
app.run(debug=True, host='0.0.0.0', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment