python - Flask 应用程序未连接到端口

标签 python flask localhost

我正在学习 Miguel Grinberg 的 Flask Web 开发教程,并且复制了这段代码

from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
    return '<h1>Hello World!</h1>'
if __name__ == '__main__':
    app.run(debug=True)

在 Jupyter 上我得到了这个结果: * 在 http://127.0.0.1:5000/ 上运行(按 CTRL+C 退出) *重新启动统计 发生异常,使用 %tb 查看完整的回溯。 系统导出:1

如果我打开 http://127.0.0.1:5000/该页面只是永远加载,如果我在 Powershell 上运行代码也是一样(但没有给出错误)。 我试图寻找类似的问题,但我找不到任何东西,你能告诉我我做错了什么以及我能做些什么来解决这个问题吗?提前致谢。

编辑: 正如 Mariusz 所问,这是我通过在单元格开头添加 %tb 得到的错误:

SystemExit                                Traceback (most recent call last)
<ipython-input-4-5757c6364c65> in <module>()
      8     return '<h1>Hello World!</h1>'
      9 if __name__ == '__main__':
---> 10     app.run(debug=True)

C:\Users\Antonio\Anaconda3\lib\site-packages\flask\app.py in run(self, host, port, debug, **options)
    841         options.setdefault('passthrough_errors', True)
    842         try:
--> 843             run_simple(host, port, self, **options)
    844         finally:
    845             # reset the first request information if the development server

C:\Users\Antonio\Anaconda3\lib\site-packages\werkzeug\serving.py in run_simple(hostname, port, application, use_reloader, use_debugger, use_evalex, extra_files, reloader_interval, reloader_type, threaded, processes,     request_handler, static_files, passthrough_errors, ssl_context)
    690         from ._reloader import run_with_reloader
    691         run_with_reloader(inner, extra_files, reloader_interval,
--> 692                           reloader_type)
    693     else:
    694         inner()

C:\Users\Antonio\Anaconda3\lib\site-packages\werkzeug\_reloader.py in run_with_reloader(main_func, extra_files, interval, reloader_type)
    248             reloader.run()
    249         else:
--> 250             sys.exit(reloader.restart_with_reloader())
    251     except KeyboardInterrupt:
    252         pass

SystemExit: 1

最佳答案

你能试试这个代码,看看它是否有效

app.run(debug=True, use_reloader=False)

关于python - Flask 应用程序未连接到端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746811/

相关文章:

python - (HTML 抓取)列的 XPath 根据颜色而变化

python - 哪个 PEP 管理 dict.values() 的顺序?

python - 以编程方式调用 wxPython EVT_BUTTON 事件

python - 将 Flask 应用程序部署到 Heroku - 找不到模板

windows-10 - 为什么我无法在手机上访问我本地的 Gatsby 站点

python - 使用 ruamel.yaml 时如何防止重新格式化 yaml 文件?

python - Flask:如何从使用 @app.route 装饰器的函数中获取返回值?

postgresql - flask 无法将 postgresql 与 docker-compose 连接

android - Android 设备 Web 服务 SYMFONY 3 的连接被拒绝

npm - webpack-dev-server 找不到本地主机