Python flask URL mapping issue, returning 404 - python

-->project
--->run.py
--->config.py
--->readme.md
--->app
--->__init__.py
--->controllers
--->__init__.py
--->test_controller.py
--->model
--->__init__.py
--->test_model1.py
--->test_model2.py
run.py
from app import app
app.run(host = '0.0.0.0', port = 8080, debug = True)
config.py - All configuration variable
app/__init__.py
from flask import Flask
app = Flask(__name__)
controllers/__init__.py - Empty
controllers/test_controller.py
#app.route('/test', methods=['POST'])
def test():
return "Hello world"
When I start my server form run.py the server gets started.
But when I try the URL http://locahost:8080/test, it returns 404.
But if the route is configured in app/___init__.py it is working.
Can anyone please guide me what is incorrect here in configuration.
I want to keep the above structure, please let me know of any issues.

Unless you import the file containing the #app.route decorator, it won't be registered. Flask won't import and register all .py files automagically for you.
At the end of your __init__.py file in app/, import projectname.controllers, and import test_controller in the __init__.py file in the controllers module.

Related

Flask 404 Error - Using Flask app Inside a class and called from a different python file - Where am I going wrong?

I have a simple flask app inside a class structure so that the flask server can be initiated by calling the call_flaskapp function :
|--folder structure
|---------flask_file.py
|---------main.py
File name is flask_file
from flask import Flask
class flask_app:
def call_flaskapp(self):
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World'
app.run(debug =True)
Here the main python file from where I am calling the function:
from flask_file import flask_app
fa = flask_app()
fa.call_flaskapp()
The server is up and running but whenever I am opening the URL I am getting a 404 error.
I might be doing a silly mistake . Please let me know
Thanks

flask socketio - server does not reload or update

I'm trying to use flask-socketio. I ran a simple example from the docs and it worked fine. I'm using a virtual environment
app.py
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
#socketio.on('message')
def handle_message(message):
print('received message: ' + message)
if __name__ == '__main__':
socketio.run(app, port=8000)
and in demo.py I use the socketio client library
demo.py
from socketIO_client import SocketIO, LoggingNamespace
def on_message_response(*args):
print('on_message_response', args)
with SocketIO('localhost', 8000, LoggingNamespace) as socketIO:
socketIO.emit('message', 'xxx', on_message_response)
socketIO.wait_for_callbacks(seconds=1)
I've installed eventlet and this basic example worked fine. When I ran I got the output received message: xxx However when I updated the app.py file and demo.py to this
app.py
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
#socketio.on('message')
def handle_message(name, age):
print('We Welcome {} who is {} years old'.format(name,age))
if __name__ == '__main__':
socketio.run(app, port=8000)
demo.py
from socketIO_client import SocketIO, LoggingNamespace
def on_message_response(*args):
print('on_message_response', args)
with SocketIO('localhost', 8000, LoggingNamespace) as socketIO:
socketIO.emit('message', {'name':'Dipanshu', 'age':25}, on_message_response)
socketIO.wait_for_callbacks(seconds=1)
It doesnt seem like the app.py reloads or is updating because I keep getting this
File "app.py", line 10, in handle_message
print('received message: ' + message) TypeError: must be str, not dict
I have restarted the app.py a few times but I get the same error. I also tried uninstalling eventlet and ran it using the Flask server like other Flask app but it was still the same.
This could be an issue with compiled Python files. I'm going to give you a couple of ideas to try:
My first suggestion is for you to delete all *.pyc files and __pycache__ directories that you have in your main application directory or in any sub-directories. This is going to remove all compiled Python files, and will force Python to recompile everything.
Alternatively, you can also take your two source files and move them to a brand new directory, and then run your server and client from the new directory. This should also clear any issues due to caching of compiled Python files.
Hope this helps!

Why does localhost:5000 not work in Flask?

I'm using flask app factory pattern like and have this run.py file:
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='localhost', debug=True)
Then I run the app like this:
python run.py
But when I go to http://localhost:5000 it doesn't work.
It 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.
What could be wrong? it works well when I have 127.0.0.1 address...
I need to run on "localhost" because I'm integrating square payments and their sandbox setup requires I make requests to their API from a 'localhost'.
Also, when I make the request in the browser, on the terminal when flask responds there is this:
127.0.0.1 - - [09/Sep/2017 00:30:45] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [09/Sep/2017 00:30:45] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [09/Sep/2017 00:30:45] "GET /favicon.ico HTTP/1.1" 404 -
So it looks like request reaches flask but flask returns 404.
Here is part of my init.py file:
# from __future__ import print_function
# import flask
from flask import Flask, render_template, url_for, redirect, flash, request, \
session, current_app, abort
import os
# flask sqlaclhemy
from sqlalchemy import func, desc, asc, or_, and_
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib.sqla import ModelView
# Flask secrutiy
from flask_security import (Security, SQLAlchemyUserDatastore,
login_required, current_user)
from flask_login import LoginManager
from flask_mail import Mail
# square connect setup
import uuid
import squareconnect
from squareconnect.rest import ApiException
# from squareconnect.apis.locations_api import LocationsApi
from squareconnect.apis.transactions_api import TransactionsApi
mail = Mail()
class CustomAdminIndexView(AdminIndexView):
def is_accessible(self):
return current_user.is_authenticated and current_user.has_role('admin')
def create_app():
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
mail.init_app(app)
from models import db, User, Role
db.init_app(app)
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
#app.route('/')
def home():
return render_template('home.html')
return app
the simple alternative solution is first to check if the port 5000 is avialable you can check that with this comand :
netstat -lat
find more about available port here :
if you are not obliged to use port 5000 you can try anything else you want ..
if every thing is ok that mean you have a problem with your home page , you don't have a route to '/' , that why you are getting the 404 error when you go to localhost:5000/ :
so to correct it you have 3 solution :
add the app.route('/') in your init.py file
add it directly in your run.py after creating the app (not a good way)
try to use blueprints
as you didn't provide your init.py code let add it to your run.py ,
from app import create_app
app = create_app()
#app.route('/')
def homepage():
return 'hello world'
if __name__ == '__main__':
app.run(host='localhost', port=9874)
another solution as suggest in comment is to check if 127.0.0.1 resolve to localhost find the host file by typing this command and check if you have the same line as mine :
nano /etc/hosts
and open the file :
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
Just incase anyone on a mac runs into this issue and has trouble finding any answers (like me), I just discovered that it's because Apple Airplay Receiver runs on port 5000. Disable airplay receiver and try again.
there will be no entry as localhost in your hosts file
example host file
127.0.0.1 localhost
you can check your hosts file in following ways
for linux
sudo vi /etc/hosts
for windows
open this file C:\Windows\System32\Drivers\etc\hosts
if there is no localhost in your hosts file add and save it.
May be you need to install virtual enviroment
pip install virtualenv
does this. Hope this works
You should try switching out localhost for 0.0.0.0.
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
This has it serve on localhost for me.
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello"
if name == "main":
app.run(host='0.0.0.0', port=9874)

Python/Flask Rest API not passing preflight w/CORS

I am trying to get the simplest rest api up that I can in python and I am having trouble. I'm not sure what I am doing wrong, but I think it has something to do with CORS. This is frustrating, as I have used the flask_cors package in order to fix this and it does not appear to work.
In my main.py file i have the following
from flask import Flask
from flask_cors import CORS, cross_origin
app = Flask(__name__)
CORS(app)
import routes.login
if __name__ == '__main__':
app.run(debug=True)
For my project I have this as my folder structure:
main.py
__init__.py
routes
__init__.py
login.py
And i have the following code in login.py
from flask import Flask
from flask_cors import CORS, cross_origin
from main import app
CORS(app)
#app.route('/login', methods=['POST', 'GET'])
#cross_origin()
def login(name, password):
if request.method == 'POST':
print('inside login POST')
if request.method == 'GET':
print('inside login GET')
I'm currently getting this error:
xhr.js:178 OPTIONS http://localhost:5000/login 404 (NOT FOUND)
dispatchXhrRequest # xhr.js:178
xhrAdapter # xhr.js:12
dispatchRequest # dispatchRequest.js:52
:3000/pictureswapper:1 XMLHttpRequest cannot load http://localhost:5000/login.
Response for preflight has invalid HTTP status code 404
There is some sort of CORS error, but I really don't know what's going wrong. Any ideas?
EDIT: The only place in the documentation that has anything to say about preflight is here (https://flask-cors.readthedocs.io/en/v1.3.1/index.html?highlight=preflight). If I add
#cross_origin(headers=['Content-Type']) # Send Access-Control-Allow-Headers
It doesn't break the application, but neither does it fix the error.
Revisiting this after a few months.
One of the nasty gotchas in python/flask appears to be that the compiled code will get cached, so if you change something at the entrypoint of the app (ie main.py), and don't delete the binaries that flask export creates then every time you run flask export and recompile it may be using old code!
Make sure to delete (in atom they are the purple 1/0 files -if you have file icons enabled- labeled .pyc) these files if you are getting spooky output.
Add OPTIONS to the methods keyword of your route decorator, or remove that keyword altogether.

Flask doesn't locate template directory when running with twisted

Following some advice that I found here I am trying to use Flask as a web interface for an application that runs with twisted.
As suggested in Flask documentation I created a "templates" directory which is at the same level as my script but when I launch the server I get the following error:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
When I do not try to load a template and just write a string in the request it works fine. This is what makes me think it is related to the load of the template.
from twisted.internet import reactor
from twisted.web.resource import Resource
from twisted.web.wsgi import WSGIResource
from twisted.internet.threads import deferToThread
from twisted.web.server import Site, NOT_DONE_YET
from flask import Flask, request, session, redirect, url_for, abort, \
render_template, flash
app= Flask(__name__)
app.config.from_object(__name__)
#app.route('/login', methods= ['GET', 'POST'])
def login():
return render_template('login.html', error= error)
if __name__ == '__main__':
root = WSGIResource(reactor, reactor.getThreadPool(), app)
factory = Site(root)
reactor.listenTCP(8880, factory)
reactor.run()
Some frameworks will change directory from your current working directory when they are run in daemon mode, and this might very well be the case here.
Flask, since 0.7, has supported passing a template_folder keyword argument when calling Flask, so you could try:
import os
tmpl_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
The following is a shorter version that will work just fine:
tmpl_dir = os.path.join(os.path.dirname(__file__), 'templates)
# ...
app = Flask('myapp', template_folder=tmpl_dir)
You can feed Jinja2 with a default templates directory (as written here) like this :
import jinja2
app = Flask(__name__)
app.jinja_loader = jinja2.FileSystemLoader('path/to/templates/directory')

Categories