为什么localhost:5000在Flask中不起作用? [英] Why does localhost:5000 not work in Flask?

查看:108
本文介绍了为什么localhost:5000在Flask中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask应用程序工厂模式,并具有以下run.py文件:

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

但是当我访问 http://localhost:5000 时,它不起作用.它说:

But when I go to http://localhost:5000 it doesn't work. It says:

未找到

在服务器上找不到请求的URL.如果输入URL手动,请检查您的拼写,然后重试.

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

可能是什么问题?当我有127.0.0.1地址时,效果很好...

What could be wrong? it works well when I have 127.0.0.1 address...

我需要在本地主机"上运行,因为我正在集成方形付款,并且其沙盒设置要求我从本地主机"向其API发出请求.

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 -

因此,请求似乎到达了烧瓶,但烧瓶返回了404.

So it looks like request reaches flask but flask returns 404.

这是我的 init .py文件的一部分:

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

推荐答案

简单的替代解决方案是首先检查端口5000是否可用,您可以使用以下命令进行检查:

the simple alternative solution is first to check if the port 5000 is avialable you can check that with this comand :

netstat -lat

此处找到更多有关可用端口的信息:如果您没有义务使用端口5000,则可以尝试任何其他您想要的..如果一切正常,这意味着您的主页有问题,则没有到'/'的路由,这就是为什么当您进入localhost:5000/时出现404错误的原因:所以要纠正它,您有3个解决方案:

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. 在您的 init .py文件

在创建应用后将其直接添加到您的run.py中(不是一个好方法)

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

尝试使用蓝图

由于您没有提供 init .py代码,请将其添加到run.py中,

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)

注释中建议的另一种解决方案是通过键入以下命令来检查127.0.0.1是否解析为localhost找到主机文件,并检查是否与我的行相同:

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

并打开文件:

##
# 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

这篇关于为什么localhost:5000在Flask中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆