python - 为什么 localhost :5000 not work in Flask?

标签 python flask

我正在使用 Flask 应用程序工厂模式,并且有这个 run.py 文件:

from app import create_app

app = create_app()

if __name__ == '__main__':
    app.run(host='localhost', debug=True)

然后我像这样运行应用程序:

python run.py

但是当我去http://localhost:5000时它不起作用。 它说:

Not Found

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 地址时它运行良好...

我需要在“localhost”上运行,因为我正在集成方形支付,并且他们的沙箱设置要求我从“localhost”向他们的 API 发出请求。

此外,当我在浏览器中发出请求时,当 Flask 响应时,终端上会出现以下内容:

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 -

所以看起来请求到达了flask,但flask返回了404。

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

# 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 是否可用;您可以使用以下命令进行检查:

netstat -lat

查找有关可用端口的更多信息 here : 如果您没有义务使用端口 5000,您可以尝试其他任何您想要的.. 如果一切正常,则说明您的主页有问题;你没有到 '/' 的路由,这就是为什么当你转到 localhost:5000/时会收到 404 错误: 因此,要纠正它,您有 3 个解决方案:

  1. init.py 文件中添加 app.route('/')

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

  3. 尝试使用蓝图

由于您没有提供 init.py 代码,让我们将其添加到您的 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;通过键入此命令找到主机文件,并检查是否有与我的相同的行:

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

关于python - 为什么 localhost :5000 not work in Flask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127005/

相关文章:

python - Windows 上的 Apache Zeppelin : error creating python interpreter

python - 限制正则​​表达式中的位数

python - 尝试使用 Gunicorn 启动 FLASK 应用程序时,Python 中的 Catch 22

python - 如何在 flask 网络应用程序中接受来自 Xively 的 POST 数据?

javascript - 动态改变 Google map 上绘制的圆圈的颜色

Python map() 函数输出到 Pandas DataFrame

python - Itertools 反向映射的方法?

python - 从 CSV 文件导入/导出嵌套字典

android - 在没有互联网的情况下从 WLAN 请求下载时,下载不会在 Android 9 上启动

python - flask 中的多语言网址