程序代码(index.py):
# 导入flask模块 from flask import Flask # __name__可能是__main__,也可能是导入模块 app = Flask(__name__) # 定义路由 @app.route('/') def hello_world(): # 返回要显示的内容 return 'hello world!!!'
有了代码如何启动呢?
打开终端程序输入下面内容(终端程序请切换到相应的文件目录):
export FLASK_APP=index.py
flask run
如果是Windows用户:
(myenv) F:deletepythonflask>set FLASK_APP=index.py (myenv) F:deletepythonflask>flask run * Serving Flask app "index.py" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [18/Jan/2019 09:57:12] "[33mGET /favicon.ico HTTP/1.1[0m" 404 - 127.0.0.1 - - [18/Jan/2019 09:57:12] "[37mGET / HTTP/1.1[0m" 200 -
在浏览器访问对应的接口即可,我这里是:127.0.0.1:5000