it throws a 404 when without url prefix (FLASK) - python

You see Im having a problem where in flask, I made a web app. and I added the URL prefix as views
and you see without /views attached to localhost it throws a 404, I wanna change it so it will redirect automatically to /views when you go to the regular URL such as http://127.0.0.1:8000/
I tried adding #app.route in app.py but it just caused even more problems

You could redirect automatically from http://127.0.0.1:8000/ to http://127.0.0.1:8000/views using the code below.
from flask import Flask, jsonify, redirect
app = Flask(__name__)
#Page 1
#app.route('/', methods=['GET'])
def welcome():
return redirect("http://127.0.0.1:8000/views", code=302)
#Page 2
#app.route('/views', methods=['GET'])
def hello():
return jsonify({"data": "Hello"})
if __name__ == '__main__':
app.run(host="0.0.0.0", port="8000")
Output
#127.0.0.1 - - [01/Dec/2022 15:23:23] "GET / HTTP/1.1" 302 - (Redirecting to views page)
#127.0.0.1 - - [01/Dec/2022 15:23:23] "GET /views HTTP/1.1" 200 -
Hope this helps. Happy Coding :)

Related

Regarding the problem of the route function according to the API object declaration location of Flask_restx

Flask restx is a library that develops api functions by dividing them into files in the flask framework and supports swaggerui.
When serving api and web pages on one flask server, the api part can be divided into files and developed using restx.
However, there is a caution when registering namespace to write flask restx.
The difference between the two source codes below is whether the namespace registration part of api and the app.route function part of api, and whether the namespace registration part of api comes before the app.route or later.
from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test
app = Flask(__name__)
#app.route('/')
def index():
return 'index'
api = Api(
app,
doc="/doc/",
version="0.1",
title="test",
)
api.add_namespace(test, '/test')
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test
app = Flask(__name__)
api = Api(
app,
doc="/doc/",
version="0.1",
title="test",
)
api.add_namespace(test, '/test')
#app.route('/')
def index():
return 'index'
if __name__ == '__main__':
app.run(debug=True)
First, when executed with the first source code, it is normally recognized when approaching the / and /test path.
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 404 -
However, the second source code recognizes only /test normally and / does not.
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 200 -
It can be seen that the console log and web browser are also displaying 404 errors when approaching / route.
However, in the second source code, not all of the subroutes of / are not possible.
from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test
app = Flask(__name__)
api = Api(
app,
doc="/doc/",
version="0.1",
title="test",
)
api.add_namespace(test, '/test')
#app.route('/')
def index():
return 'index'
#app.route('/main')
def main():
return 'main'
if __name__ == '__main__':
app.run(debug=True)
In this way, / root is not recognized, but /main recognizes.
127.0.0.1 - - [27/Sep/2021 15:40:35] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:45] "GET /main HTTP/1.1" 200 -
I don't know why they do this.
Try leaving the route string empty to see if that works
#app.route('')
def index():
return 'index'

Flask application showing 404

I am new to Python and Flask and I need to work on a code base. I have the following files in directory called migration
Name
app
env
__pycache__
requirements.txt
run.py
In the run.py, I have a the following code:
from app import app
app.run(debug=True)
and inside the app directory, I have one __init__.py, which as code :
from app.helpers import get_page_display_name, get_page_url_name
# from app import views
from flask import Flask
app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False
app.jinja_env.globals.update(get_page_display_name=get_page_display_name)
app.jinja_env.globals.update(get_page_url_name=get_page_url_name)
Now I have a views.py file inside the same app folder and it has the route configuration and corresponding code like :
#app.route('/')
def index():
if 'username' in session:
return render_template("index.html")
return redirect(url_for('login'))
I am trying to run the application. I have used the following commands:
env/Scripts/activate
This has activated the environment and then:
$env:FLASK_APP=.\run.py
flask run
This has shown a message like it is running on http://127.0.0.1:5000 and printed the following messages when I opened the URL in browser:
127.0.0.1 - - [19/Nov/2020 10:23:15] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:15] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:16] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:17] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:18] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:18] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [19/Nov/2020 10:23:18] "GET / HTTP/1.1" 404 -
I have also tried with
$env:FLASK_APP=.\app\views.py
flask run
This also has printed the same message as it is running on the same port, but when opened, the same 404 messages are being shown.
How can I run this application ? I have checked the documentation, but the structure is little different for this application. Thanks in advance for the help.
You have to import your views file after definition of app. I suggest you to use blueprint.
EDIT
views.py
from flask import Blueprint
bp = Blueprint('test', __name__, url_prefix='/')
#bp.route('/')
def index():
if 'username' in session:
return render_template("index.html")
return redirect(url_for('login'))
__init__py
from app.helpers import get_page_display_name, get_page_url_name
from views import bp
from flask import Flask
app = Flask(__name__)
app.register_blueprint(bp)
app.config['JSON_SORT_KEYS'] = False
app.jinja_env.globals.update(get_page_display_name=get_page_display_name)
app.jinja_env.globals.update(get_page_url_name=get_page_url_name)

Flask 404 Not Found ( 33mGET / HTTP/1.1 [0m" 404 - )

I am trying to create my first script with flask.
Here is my code:
from flask import Flask
from flask import Blueprint, request
prediction_app = Blueprint('prediction_app', __name__)
#prediction_app.route('/health', methods=['GET'])
def health():
if request.method == 'GET':
return 'ok'
def create_app() -> Flask:
"""Create a flask app instance."""
flask_app = Flask('ml_api')
# import blueprints
flask_app.register_blueprint(prediction_app)
return flask_app
application = create_app()
if __name__ == '__main__':
application.run()
I run this code as python run.py and I am getting "Running on http://127.0.0.1:5000/".
I go to this link and I am getting instead of "ok" a page with the next error:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
Command promt gives the following output:
127.0.0.1 - - [17/Jun/2020 16:59:25] "[33mGET / HTTP/1.1[0m" 404 -
Where is the problem?
I don't see a default route (/) defined; did you try pointing your browser at http://localhost:5000/health? That's the route you did define.
(localhost and 127.0.0.1 are typically equivalent, by the way...)

Flask Keep Alive Connection Request Failed

I want to crate Keep-Alive http connection, but i failed.
I build a demo app.
from flask import Flask, make_response, Response
from flask import jsonify
try:
from http.server import BaseHTTPRequestHandler
except:
from BaseHTTPServer import BaseHTTPRequestHandler
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def hello_world():
resp = make_response("{'123':'aaa'}")
return resp
if __name__ == '__main__':
BaseHTTPRequestHandler.protocol_version = "HTTP/1.1"
app.run()
I send some requests,:
{"text":-1193959466}
{"text":-1139614796}
{"text":837415749}
{"text":-1220615319}
{"text":-1429538713}
{"text":118249332}
{"text":-951589224}
and i received some error:
127.0.0.1 - - [18/Apr/2019 20:14:15] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [18/Apr/2019 20:14:16] "{"text":-1193959466}POST / HTTP/1.1" 405 -
127.0.0.1 - - [18/Apr/2019 20:14:16] "{"text":-1139614796}POST / HTTP/1.1" 405 -
127.0.0.1 - - [18/Apr/2019 20:14:17] "{"text":837415749}POST / HTTP/1.1" 405 -
127.0.0.1 - - [18/Apr/2019 20:14:17] "{"text":-1220615319}POST / HTTP/1.1" 405 -
127.0.0.1 - - [18/Apr/2019 20:14:18] "{"text":-1429538713}POST / HTTP/1.1" 405 -
127.0.0.1 - - [18/Apr/2019 20:14:19] "{"text":118249332}POST / HTTP/1.1" 405 -
127.0.0.1 - - [18/Apr/2019 20:14:19] "{"text":-951589224}POST / HTTP/1.1" 405 -
for this log, first request is success, but others failed.
It does not seem to clear the last request content.
if i remove this code:
BaseHTTPRequestHandler.protocol_version = "HTTP/1.1"
it's ok again.
Has anyone encountered the same problem? i used flask version : 1.0.2
update:
i know what happened, i need to read the request contents:
#app.route('/', methods=['POST'])
def hello_world():
# read the request content
print(request.json)
print("\n")
resp = make_response("{'123':'aaa'}")
return resp
thanks all.
Instead of using BaseHTTPRequestHandler, you can employ the default request_handler WSGIRequestHandler.
Since WSGIRequestHandler extends BaseHTTPRequestHandler, you can specify the HTTP protocol version you want to use. If you set the property to HTTP/1.1, the connection will stay alive.
from flask import Flask, make_response, Response
from werkzeug.serving import WSGIRequestHandler
from flask import jsonify
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def hello_world():
resp = make_response("{'123':'aaa'}")
return resp
if __name__ == '__main__':
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run()
Don't forget to include from werkzeug.serving import WSGIRequestHandler

flask app gives 404 for non-root route

I want to give flask a try. Using flask 0.12, python 3.4
I've created the project tree similar like in:
https://damyanon.net/post/flask-series-structure/
controllers.py code:
from flask import Blueprint
import functools, operator
main = Blueprint('main', __name__)
#main.route('/')
def index():
return "Main world"
#main.route('/foo')
def foo():
return "this is foo"
when I run the app,
I got 404 for /foo route but '/' is OK
* Serving Flask app "run"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [27/Dec/2017 15:19:21] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Dec/2017 15:19:25] "GET /foo HTTP/1.1" 404 -
Any clue?
Thanks.
edit:
As requested, here how I register blueprint in
flask_app/localservice/init.py. Not sure about application factory. I'm still new with this. I substitute bookshelf with localservice and not use admin
from flask import Flask
from localservice.main.controllers import main
app = Flask(__name__)
app.register_blueprint(main, url_prefix='/')
I was following the same tutorial and the same error occurred for me as well. After being stuck on this for quite a while I finally figured it out.
So looks like there's an error in the tutorial. You can't register using '/'.
app.register_blueprint(main, url_prefix='/')
This is the actual github codebase where the tutorial guy wrote the code.
If you look at the code and commit history, he changed url_prefix from '/' to 'main'. Change your url_prefix and the code should work.
If you don't insist on following that tutorial, the code in the Flask Quickstart docs works perfectly fine
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return "Main world"
#app.route('/foo')
def foo():
return "this is foo"
if __name__ == '__main__':
app.run()

Categories