10

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
8oh8
  • 1,247
  • 5
  • 19
  • 35

7 Answers7

16

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.

Robert Guice
  • 161
  • 1
  • 2
10

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:

  1. add the app.route('/') in your init.py file

  2. add it directly in your run.py after creating the app (not a good way)

  3. 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
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
  • 1
    I do have a line for localhost in hosts file. Also tried it with a different port. Still doesn't work. – 8oh8 Sep 09 '17 at 07:32
  • I can see the flask server receiving the get request, but still everything returns 404 from flask. – 8oh8 Sep 09 '17 at 07:32
  • If tou see the flask server that mean your server is running, tell me do you have a route to home?? show me your view function. Let me edit the answer – Espoir Murhabazi Sep 09 '17 at 07:43
  • Hi, I edited the question and included part of my __init__.py file. I also tried just adding it to the run.py file and still doesn't go throught. It shows up in the flask server log but says 404 not found :( – 8oh8 Sep 09 '17 at 22:03
  • Yes, if I run it on 127.0.0.1 it works, it shows my home. Html but if I change it to "localhost", it reaches flask but flask returns 404. – 8oh8 Sep 10 '17 at 05:03
  • Are you sure you want to use only 'localhost' in your api?? Andy orher domain name like : "myapp.com" can works ?? If yes let me know i have a suggestion . – Espoir Murhabazi Sep 10 '17 at 05:14
  • Yes, I have to use "localhost" it's a requirement by square payments. Or else the API won't work unless it's live but I gotta test first because it's payment processing. – 8oh8 Sep 10 '17 at 16:44
  • Have you tried binding to all IPs instead of a hostname? `app.run('0.0.0.0')` – dirn Sep 10 '17 at 18:07
  • I don't think that will help me because I need to be able to go to localhost in my browser, actually type in localhost instead of 0.0.0.0. – 8oh8 Sep 10 '17 at 18:25
  • It will help you. Try it. – dirn Sep 10 '17 at 19:30
  • Give us an ideas about your etc/hosts file – Espoir Murhabazi Sep 10 '17 at 19:39
  • 1
    Ok, I figured it out, I had this in my config.py: SERVER_NAME = 'localhost', i removed that line and it worked. – 8oh8 Sep 10 '17 at 20:32
1

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.

suhail areekkan
  • 1,646
  • 1
  • 13
  • 16
  • On Windows, if Docker Desktop is installed the mapping is overwritten by it. I changed the mapping back to localhost and it worked. – Jakub Małecki Nov 24 '21 at 08:43
1

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.

DKatri
  • 11
  • 1
0
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)

  • 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) – Yuva Lekha Sep 14 '21 at 08:05
  • mention (host ='localhost' port = 5000) – Yuva Lekha Sep 14 '21 at 08:06
  • or else port = 9874 – Yuva Lekha Sep 14 '21 at 08:06
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 14 '21 at 08:48
0

everything is working fine for me when I give port 5000 to the localhost as you can see here enter image description here

Tanveer Ahmad
  • 706
  • 4
  • 12
-1

May be you need to install virtual enviroment

pip install virtualenv

does this. Hope this works

Sajin Shereef
  • 119
  • 1
  • 1
  • 7
  • This is good practice when creating flask projects. I would add that a virtual environment needs to be created as well using $virtualenv ['filename'], not sure if this fixes the localhost issue though – Mwspencer Mar 20 '18 at 20:32