Reputation: 1367
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
Upvotes: 17
Views: 97011
Reputation: 31
For Mac users like me who spent way too much time reading through answers to find the solution: In 2004 Apple started using port 5000 and starting with MacOS Monterey the Airplay occupied port 5000 and the default state was changed to ON. This means you can't use airplay and dev on that port simultaneously. Someone mentioned this above, but I wanted to show the exact toggle to turn this off in a screenshot in 2024 for OS Sonoma.
Apple forum link: https://forums.developer.apple.com/forums/thread/682332
Upvotes: 2
Reputation: 1215
everything is working fine for me when I give port 5000 to the localhost as you can see here
Upvotes: 0
Reputation: 6376
The simple alternative solution is first to check if port 5000 is available; you can check that with this command:
netstat -lat
find more about available ports here : if you are not obliged to use port 5000 you can try anything else you want .. if everything is ok, that means you have a problem with your home page; you don't have a route to '/' , and that is why you are getting the 404 error when you go to localhost:5000/ : So to correct it, you have 3 solutions:
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's 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 suggested in the comment, is to check if 127.0.0.1 resolves 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
Upvotes: 11
Reputation: 261
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.
Upvotes: 26
Reputation: 1
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)
Upvotes: 0
Reputation: 19
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.
Upvotes: 1
Reputation: 119
May be you need to install virtual enviroment
pip install virtualenv
does this. Hope this works
Upvotes: -1
Reputation: 1956
there will be no entry as localhost in your hosts file
127.0.0.1 localhost
you can check your hosts file in following ways
sudo vi /etc/hosts
open this file C:\Windows\System32\Drivers\etc\hosts
if there is no localhost in your hosts file add and save it.
Upvotes: 1