flask的脚本控制flask_script
flask_script用法:
#_*_ encoding: utf-8 _*_ @author: ty hery 2019/1/12
from flask_script import Manager
from flask import Flask
import sqlite3
app = Flask(__name__)
manager = Manager(app)
@manager.command
def hello():
print('hello world')
@manager.option('-m','--msg',dest='msg_val',default='world') # -m :参数 default:默认参数
def hello_world(msg_val):
print('hello ' + msg_val)
@manager.command
def init_db():
sql = 'create table user (id INT,name TEXT)'
conn = sqlite3.connect("flask_test.db")
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
if __name__ == "__main__":
manager.run()
在该文件同等级目录下: python manager.py hello 打印出 hello world
python manager.py hello 打印出 hello world
(py2_for_websocket) X:\python3.64\Flask>python manager.py hello_world 运行 hello world函数不带参数,使用默认参数,
hello world
(py2_for_websocket) X:\python3.64\Flask>python manager.py hello_world -m jikexueyuan 运行 hello world函数带参数jikexueyuan
hello jikexueyuan
(py2_for_websocket) X:\python3.64\Flask>python manager.py init_db # 在当前目录下新建了flask_test.db文件
#_*_ encoding: utf-8 _*_ @author: ty hery 2019/12/20
from flask import Flask, session, current_app, g
from flask_script import Manager # 启动命令的管理器类
app = Flask(__name__)
# 创建Manager管理器类的对象
manager = Manager(app)
@app.route('/index')
def index():
print('index 被执行')
return 'index page'
if __name__ == '__main__':
# print('--哈哈01--',app.url_map,'--哈哈01--')
# app.run(debug=True)
# app.run(debug=False)
# 通过管理器对象来启动flask
manager.run()
可以在这个文件所在目录下 运行: python 10_flask_script.py runserver -h 127.0.0.1 -p 5000
启动项目文件方式: -d指的是开启debug模式 在 X:\python3.64\anzhuangweizhi目录(pyhton3.6的默认环境下启动python
python X:/python3.64/Flask/jikexueyuan_flask/10_flask_script.py runserver - h 0.0.0.0 - p 8000 - d
X:\python3.64\Flask\flask类视图和restful>python 10_flask_script.py runserver -h 127.0.0.1 -p 5000 启动命令
- Serving Flask app "10_flask_script" (lazy loading)
- Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
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 - - [12/Apr/2021 22:25:18] "GET /index/ HTTP/1.1" 404 -
127.0.0.1 - - [12/Apr/2021 22:27:36] "GET /index HTTP/1.1" 200 -
127.0.0.1 - - [12/Apr/2021 22:27:37] "GET /favicon.ico HTTP/1.1" 404 -
写入自己的博客中才能记得长久