I am attempting to run a flask backend server with flask. Whenever I run:
python3 server.py
The server hosts and whenever I go to it, it gives an error 404 not found. Here is the code:
from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
@app.before_request
def before_request():
print(f"Request path: {request.path}")
@app.route('/execute-file/', methods=['POST'])
def execute_file():
try:
# Get the file name from the request body
fileName = request.form.get('fileName')
if not fileName:
return jsonify({'success': False, 'error': 'File name not provided'})
# Execute the file
result = subprocess.run(['python', f'lib/execute/{fileName}'], capture_output=True, text=True)
if result.returncode == 0:
return jsonify({'success': True, 'output': result.stdout})
else:
return jsonify({'success': False, 'error': result.stderr})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
The error itself says: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. console displays the following:
* Serving Flask app 'server'
* Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://192.168.1.21:8000
Press CTRL+C to quit
Request path: / 127.0.0.1 - - [20/Mar/2025 19:11:22] "GET / HTTP/1.1" 404 -
I am attempting to run a flask backend server with flask. Whenever I run:
python3 server.py
The server hosts and whenever I go to it, it gives an error 404 not found. Here is the code:
from flask import Flask, request, jsonify
from flask_cors import CORS
import subprocess
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
@app.before_request
def before_request():
print(f"Request path: {request.path}")
@app.route('/execute-file/', methods=['POST'])
def execute_file():
try:
# Get the file name from the request body
fileName = request.form.get('fileName')
if not fileName:
return jsonify({'success': False, 'error': 'File name not provided'})
# Execute the file
result = subprocess.run(['python', f'lib/execute/{fileName}'], capture_output=True, text=True)
if result.returncode == 0:
return jsonify({'success': True, 'output': result.stdout})
else:
return jsonify({'success': False, 'error': result.stderr})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
The error itself says: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. console displays the following:
* Serving Flask app 'server'
* Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:8000
* Running on http://192.168.1.21:8000
Press CTRL+C to quit
Request path: / 127.0.0.1 - - [20/Mar/2025 19:11:22] "GET / HTTP/1.1" 404 -
Share
Improve this question
asked Mar 20 at 13:58
Ojasvi SinglaOjasvi Singla
1
1
|
1 Answer
Reset to default 0If you are accessing 127.0.0.1:8000
through a browser, you need to define a route for /
to handle the request.
Define it like this:
@app.route('/', methods=['GET'])
def home():
return "Hello, world!"
Additionally, if you want to access your currently defined route:
@app.route('/execute-file/', methods=['POST'])
...
you can consider using Postman to conveniently test your API(i.e., set input url as 127.0.0.1:8000/execute-file
and set method as POST
).
/
norGET
. – Klaus D. Commented Mar 20 at 14:02