localhost:5000
http://localhost:5000
Port 5000 is the default development server port for Flask, a lightweight Python web framework. It's also used by other Python applications, Webpack Dev Server, Docker Registry, and various development tools.
→ Open localhost:5000
What Runs on Port 5000?
- Flask - Python micro web framework
- Werkzeug - Flask's underlying development server
- Docker Registry - Container image registry
- Synology NAS - DSM management interface
- UPnP/SSDP - Universal Plug and Play discovery
- Webpack Dev Server - Alternative HTTP port
- Python HTTP servers - Custom development servers
- Svelte Kit - JavaScript framework (sometimes uses 5000)
Start Flask Development Server
Basic Flask Application
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '<h1>Flask app running on port 5000!</h1>'
@app.route('/api/users')
def get_users():
return {'users': ['Alice', 'Bob', 'Charlie']}
if __name__ == '__main__':
app.run(debug=True)
# Starts on http://localhost:5000
# Run the app
python app.py
# Or use flask command
flask run
Flask Run Command Options
# Basic flask run (starts on port 5000)
flask run
# Run with auto-reload on code changes
flask run --reload
# Enable debug mode
flask run --debug
# Bind to all network interfaces
flask run --host=0.0.0.0
# Use different port
flask run --port=5001
# Combine options
flask run --host=0.0.0.0 --port=5000 --debug
Set Flask Environment Variables
# Windows Command Prompt
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
# Windows PowerShell
$env:FLASK_APP = "app.py"
$env:FLASK_ENV = "development"
flask run
# Linux/Mac
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
# Or use .env file with python-dotenv
# .env
FLASK_APP=app.py
FLASK_ENV=development
FLASK_RUN_PORT=5000
FLASK_RUN_HOST=0.0.0.0
Change Flask Port from 5000
Command Line Options
# Run on different port
flask run --port=5001
flask run -p 5001
# Or set environment variable
export FLASK_RUN_PORT=5001
flask run
In Python Code
# app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Flask on custom port'
if __name__ == '__main__':
# Specify port in run()
app.run(port=5001, debug=True)
# Or use environment variable
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Fix "Port 5000 Already in Use"
Error: OSError: [Errno 48] Address already in use
Port 5000 is occupied by another process.
- Another Flask app is running
- Previous Flask instance didn't shut down
- macOS AirPlay Receiver uses port 5000 (common issue)
- Docker Registry running on port 5000
- Other Python application using port 5000
Disable macOS AirPlay Receiver
# macOS Monterey and later use port 5000 for AirPlay
# System Preferences > Sharing > Uncheck "AirPlay Receiver"
# Or run Flask on different port
flask run --port=5001
Find and Kill Process on Port 5000
Windows
# Find process using port 5000
netstat -ano | findstr :5000
# Kill process by PID
taskkill /PID [process_id] /F
# Kill all Python processes (use with caution)
taskkill /IM python.exe /F
Linux/Mac
# Find process on port 5000
lsof -i :5000
sudo lsof -i :5000
# Kill process directly
lsof -ti:5000 | xargs kill -9
sudo lsof -ti:5000 | xargs kill -9
# Alternative with fuser
sudo fuser -k 5000/tcp
# Check all Python processes
ps aux | grep python
Flask Application Structure
# Simple Flask app structure
project/
├── app.py # Main application file
├── requirements.txt # Python dependencies
├── .env # Environment variables
├── templates/ # HTML templates
│ └── index.html
├── static/ # CSS, JS, images
│ ├── css/
│ ├── js/
│ └── images/
└── venv/ # Virtual environment
# requirements.txt
Flask==2.3.0
python-dotenv==1.0.0
# Install dependencies
pip install -r requirements.txt
Flask with Templates
# app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', title='Home')
@app.route('/about')
def about():
return render_template('about.html', title='About')
if __name__ == '__main__':
app.run(debug=True)
# templates/index.html
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<h1>Welcome to Flask!</h1>
<p>Running on http://localhost:5000</p>
</body>
</html>
Flask RESTful API Example
# api.py
from flask import Flask, jsonify, request
app = Flask(__name__)
# Sample data
users = [
{'id': 1, 'name': 'Alice', 'email': 'alice@example.com'},
{'id': 2, 'name': 'Bob', 'email': 'bob@example.com'}
]
@app.route('/api/users', methods=['GET'])
def get_users():
return jsonify({'users': users})
@app.route('/api/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
user = next((u for u in users if u['id'] == user_id), None)
if user:
return jsonify(user)
return jsonify({'error': 'User not found'}), 404
@app.route('/api/users', methods=['POST'])
def create_user():
data = request.get_json()
new_user = {
'id': len(users) + 1,
'name': data.get('name'),
'email': data.get('email')
}
users.append(new_user)
return jsonify(new_user), 201
if __name__ == '__main__':
app.run(debug=True)
# Test API endpoints
# GET http://localhost:5000/api/users
# GET http://localhost:5000/api/users/1
# POST http://localhost:5000/api/users
Enable CORS in Flask
# Install Flask-CORS
pip install flask-cors
# app.py
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
# Enable CORS for all routes
CORS(app)
# Or enable for specific routes
CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}})
# Or configure CORS manually
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
@app.route('/api/data')
def get_data():
return {'message': 'CORS enabled'}
if __name__ == '__main__':
app.run(debug=True)
Flask with Database
# Install SQLAlchemy
pip install flask-sqlalchemy
# app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
# Define model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True)
# Create database
with app.app_context():
db.create_all()
@app.route('/api/users')
def get_users():
users = User.query.all()
return {'users': [{'id': u.id, 'name': u.name} for u in users]}
if __name__ == '__main__':
app.run(debug=True)
Check if Port 5000 is in Use
# Windows
netstat -ano | findstr :5000
# Linux/Mac
lsof -i :5000
netstat -an | grep 5000
ss -tulpn | grep 5000
# Check all listening ports
netstat -an | grep LISTEN
# Windows PowerShell
Test-NetConnection -ComputerName localhost -Port 5000
Deploy Flask Application
Production Server (Gunicorn)
# Install Gunicorn
pip install gunicorn
# Run with Gunicorn
gunicorn -w 4 -b 0.0.0.0:5000 app:app
# -w 4: 4 worker processes
# -b: bind to host:port
# app:app: module:application
# Run in background
gunicorn -w 4 -b 0.0.0.0:5000 app:app --daemon
Deploy with Docker
# Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"]
# Build and run
docker build -t flask-app .
docker run -p 5000:5000 flask-app
# Access at http://localhost:5000
Common Port 5000 Issues
Flask app won't start - address in use
- Check for processes on port 5000
- Disable macOS AirPlay Receiver
- Use different port with --port flag
- Kill previous Flask instances
Cannot access localhost:5000 from browser
- Verify Flask app is running
- Check for firewall blocking
- Try 127.0.0.1:5000 instead
- Review Flask console for errors
Changes not reflecting (no live reload)
- Enable debug mode:
flask run --debug
- Use
app.run(debug=True) in code
- Clear browser cache
- Hard refresh (Ctrl+F5)
Development Note:
Flask's built-in server on port 5000 is for development only. Use production WSGI servers like Gunicorn or uWSGI with Nginx reverse proxy for production deployments.
Frequently Asked Questions
Why does Flask use port 5000?
Port 5000 was chosen as Flask's default because it's above privileged ports (1-1024), unlikely to conflict with common services, and easy to remember.
Why is port 5000 already in use on Mac?
macOS Monterey (12.0) and later use port 5000 for AirPlay Receiver by default. Disable it in System Preferences > Sharing or use a different port for Flask.
How do I run Flask on port 80?
Run sudo flask run --port=80 (requires root). Better approach: use Nginx reverse proxy to forward port 80 to Flask on 5000.
Can I run multiple Flask apps simultaneously?
Yes, run each app on different port: app1 on 5000, app2 on 5001, etc. Use flask run --port flag or set in code.
How do I access Flask from mobile device?
Run flask run --host=0.0.0.0 and access using computer's IP address (e.g., http://192.168.1.100:5000) from mobile on same network.
Related Ports and Resources