树莓派-flask服务器的安装及简单应用

Flask的安装

pip install Flask

pip install Flask-cache

一个简单的后台脚本:

#!/usr/bin/env python2
import motor
from flask import Flask, render_template, request
from flask_cache import Cache

app = Flask(__name__)
#global g_car_speed_control
#global g_car_state
app.config['CACHE_TYPE'] = 'simple'
app.config['CACHE_DEFAULT_TIMEOUT'] = 5 * 600
cache = Cache(app)

@app.route("/")
def index():
    return render_template('index.html')

@app.route('/action_speed', methods=['GET', 'POST'])
def action_speed():
    car_speed=request.get_data()
    cache.set('car_speed', car_speed, timeout=99999)
    #request.form.get('speed')
    print('speed %s' %car_speed)
    process_action()
    return str(car_speed)

...
def process_action():
    car_state=cache.get('car_state')
    car_speed=cache.get('car_speed')
    #g_car_speed_control = request.form['speed']
    if car_state == 'action_stop':
        motor.brake()
    elif car_state == 'action_run':
        motor.run(int(car_speed))

  

备注:
要使用Flash cache,还需要修改/usr/local/lib/python2.7/dist-packages/flask_cache/jinja2ext.py,将 flask.ext.cache改成flask_cache,否则cache模块不能使用。

猜你喜欢

转载自www.cnblogs.com/herman1221/p/11761695.html