[英]Why does localhost:5000 not work in Flask?
我正在使用 flask 应用程序工厂模式,并有这个 run.py 文件:
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='localhost', debug=True)
然后我像这样运行应用程序:
python run.py
但是当我 go 到http://localhost:5000它不起作用。 它说:
未找到
在服务器上找不到请求的 URL。 如果您手动输入 URL,请检查您的拼写并重试。
有什么问题吗? 当我有 127.0.0.1 地址时它工作得很好......
我需要在“localhost”上运行,因为我正在集成 square payments 并且他们的沙箱设置要求我从“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
在此处找到有关可用端口的更多信息:如果您没有义务使用端口5000,则可以尝试其他所需的任何方法..如果一切正常,这意味着您的主页有问题,那么您就没有通往“ /',这就是为什么当您进入localhost:5000 /时得到404错误的原因:因此,要纠正它,您有3个解决方案:
在您的init .py文件中添加app.route('/')
创建应用程序后将其直接添加到您的run.py中(不是一个好方法)
尝试使用蓝图
由于您没有提供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
可能是您需要安装虚拟环境
pip安装virtualenv
做这个。 希望这行得通
您的主机文件中将没有作为localhost的条目
127.0.0.1 localhost
您可以通过以下方式检查主机文件
sudo vi /etc/hosts
open this file C:\Windows\System32\Drivers\etc\hosts
如果您的主机文件中没有localhost,则添加并保存它。
您应该尝试将localhost
为0.0.0.0
。
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
这对我来说可以在本地主机上使用。
from flask import Flask
app = Flask(__name__)
@app.route("/") def hello(): 如果name == " main " 返回 "Hello": app.run(host='0.0.0.0', port=9874)
以防有人在 Mac 上遇到这个问题并且找不到任何答案(比如我),我刚刚发现这是因为 Apple Airplay Receiver 在端口 5000 上运行。禁用 airplay 接收器并重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.