I’m having trouble with my Flask backend for a Flutter app. When I start the server using python3 server.py
, it seems to run fine. But when I try to access it, I get a 404 error.
Here’s my Flask code:
from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
app = FlaskApp(__name__)
CORS(app)
@app.before_request
def log_request():
print(f"Incoming request: {request.path}")
@app.route('/run-script/', methods=['POST'])
def run_script():
try:
script_name = request.form.get('script_name')
if not script_name:
return jsonify({'status': 'error', 'message': 'Script name missing'})
result = subprocess.run(['python', f'scripts/{script_name}'], capture_output=True, text=True)
if result.returncode == 0:
return jsonify({'status': 'success', 'output': result.stdout})
else:
return jsonify({'status': 'error', 'message': result.stderr})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
The server console shows:
* Running Flask app 'server'
* Debug mode: off
* Running on http://127.0.0.1:5000
* Running on http://192.168.1.100:5000
Incoming request: /
127.0.0.1 - - [21/Mar/2025 10:15:30] "GET / HTTP/1.1" 404 -
Why am I getting a 404 error and how can I fix it?