Flask HelloWorld

发布于:2022-12-31 ⋅ 阅读:(246) ⋅ 点赞:(0)

安装

pip install Flask==2.1.2

Successfully installed Flask-2.1.2 Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.2.2 click-8.1.3 colorama-0.4.5 importlib-metadata-4.12.0 itsdangerous-2.1.2 typing-extensions-4.3.0 zipp-3.8.1

代码

# hello.py
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'

运行

1、命令行启动

Flask提供了一个run命令,用开发服务器运行应用程序。

警告:部署到生产环境时,不要使用开发服务器。

 flask 命令帮助

Usage: flask [OPTIONS] COMMAND [ARGS]...                                      
                                                                              
  A general utility script for Flask applications.                            
                                                                              
  An application to load must be given with the '--app' option, 'FLASK_APP'   
  environment variable, or with a 'wsgi.py' or 'app.py' file in the current   
  directory.                                                                  
                                                                              
Options:                                                                      
  -e, --env-file FILE   Load environment variables from this file. python-    
                        dotenv must be installed.                             
  -A, --app IMPORT      The Flask application or factory function to load, in 
                        the form 'module:name'. Module can be a dotted import 
                        or file path. Name is not required if it is 'app',    
                        'application', 'create_app', or 'make_app', and can be
                        'name(args)' to pass arguments.                       
  --debug / --no-debug  Set debug mode.                                       
  --version             Show the Flask version.                               
  --help                Show this message and exit.                           
                                                                              
Commands:                                                                     
  db      Perform database migrations.                                        
  routes  Show the routes for the app.                                        
  run     Run a development server.                                           
  shell   Run a shell in the app context. 

flask run 命令帮助

Usage: flask run [OPTIONS]

  Run a local development server.

  This server is for development purposes only. It does not provide the
  stability, security, or performance of production WSGI servers.

  The reloader and debugger are enabled by default with the '--debug' option.

Options:
  -h, --host TEXT                 The interface to bind to.
  -p, --port INTEGER              The port to bind to.
  --cert PATH                     Specify a certificate file to use HTTPS.
  --key FILE                      The key file to use when specifying a
                                  certificate.
  --reload / --no-reload          Enable or disable the reloader. By default
                                  the reloader is active if debug is enabled.
  --debugger / --no-debugger      Enable or disable the debugger. By default
                                  the debugger is active if debug is enabled.
  --with-threads / --without-threads
                                  Enable or disable multithreading.
  --extra-files PATH              Extra files that trigger a reload on change.
                                  Multiple paths are separated by ';'.
  --exclude-patterns PATH         Files matching these fnmatch patterns will
                                  not trigger a reload on change. Multiple
                                  patterns are separated by ';'.
  --help                          Show this message and exit.

常用选项

  • --app:指向应用程序。如果文件命名为 app.py 或者 wsgi.py,则不必使用--app。
  • --debug:启用调试模式。在调试模式下,此服务器提供了一个交互式调试器,而且当代码发生更改时将重新加载。
  • -h / --host:指定ip。默认127.0.0.1,若需要局域网内设备可见,则设为 0.0.0.0,通过终端 ipconfig 查询IPv4 地址即可访问。
  • -p / --port:指定端口。默认 5000
# 简单
flask run

# 选项
flask --app hello --debug run -h 127.0.0.2 -p 5002

# 示例
flask --app hello --debug run

* Serving Flask app 'hello'
 * Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 135-295-389

现在访问:http://127.0.0.1:5000/,可以看到hello world。

端口被占用,linux系统查看冲突端口程序。

netstat -nlp | grep 5000

调试模式下,在请求过程中出现错误,服务器将在浏览器中显示一个交互式调试器。

2、代码启动

if __name__ == '__main__':
    app.run(debug=True)
    # app.run(debug=True, host=127.0.0.1, port=5000)

本文含有隐藏内容,请 开通VIP 后查看