Python 为什么 localhost:5000 在 Flask 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46127005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does localhost:5000 not work in Flask?
提问by 8oh8
I'm using flask app factory pattern like and have this run.py file:
我正在使用 Flask 应用程序工厂模式,并拥有这个 run.py 文件:
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:5000it doesn't work. It says:
但是当我访问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.
未找到
在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。
What could be wrong? it works well when I have 127.0.0.1 address...
可能有什么问题?当我有 127.0.0.1 地址时它运行良好......
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'.
我需要在“本地主机”上运行,因为我正在集成方形支付,他们的沙箱设置要求我从“本地主机”向他们的 API 发出请求。
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.
所以看起来请求到达了flask但是flask返回了404。
Here is part of my init.py file:
这是我的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
回答by Esptheitroad Murhabazi
the simple alternative solution is first to check if the port 5000 is avialable you can check that with this comand :
简单的替代解决方案是首先检查端口 5000 是否可用,您可以使用以下命令进行检查:
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 :
在此处找到有关可用端口的更多信息:如果您没有义务使用端口 5000,您可以尝试其他任何您想要的东西。 /' ,这就是为什么当您转到 localhost:5000/ 时会收到 404 错误的原因:因此要纠正它,您有 3 个解决方案:
add the app.route('/') in your init.py file
add it directly in your run.py after creating the app (not a good way)
try to use blueprints
在init.py 文件中添加 app.route('/')
创建应用程序后直接将其添加到您的 run.py 中(不是一个好方法)
尝试使用蓝图
as you didn't provide your init.py code let add it to your 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)
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 :
评论中建议的另一个解决方案是检查 127.0.0.1 是否解析为 localhost 通过键入此命令找到主机文件,并检查您是否与我的行相同:
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
回答by Sajin Shereef
May be you need to install virtual enviroment
可能需要安装虚拟环境
pip install virtualenv
pip 安装 virtualenv
does this. Hope this works
做这个。希望这有效
回答by DKatri
You should try switching out localhost
for 0.0.0.0
.
你应该尝试切换出localhost
的0.0.0.0
。
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
This has it serve on localhost for me.
这让它在本地主机上为我服务。
回答by suhail areekkan
there will be no entry as localhostin your hosts file
在您的主机文件中将没有作为localhost 的条目
example host file
示例主机文件
127.0.0.1 localhost
you can check your hosts file in following ways
您可以通过以下方式检查您的主机文件
for linux
用于 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.
如果您的主机文件中没有 localhost 添加并保存它。