Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does localhost:5000 not work in Flask?

Tags:

python

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
like image 514
8oh8 Avatar asked Sep 09 '17 04:09

8oh8


2 Answers

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 :

  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 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
like image 124
Espoir Murhabazi Avatar answered Sep 18 '22 19:09

Espoir Murhabazi


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.

like image 38
suhail areekkan Avatar answered Sep 20 '22 19:09

suhail areekkan