树莓派flask-restful风格的服务器demo

from flask import Flask
from flask_restful import reqparse, Api, Resource, request
import serial
import time

app = Flask(__name__)
api = Api(app)

value1 = 'welcome'
value2 = 'welcome'
value3 = 'welcome'

global serial = serial.Serial('COM9',9600,timeout=520) 
TODOS = {
    'todo1': a,
    'todo2': {'task': value2},
    'todo3': {'task': value3},
}
parser = reqparse.RequestParser()
parser.add_argument('task')

# Todo
# 增删改查
class holder_Control(Resource):
    def get(self, todo_id):
        try:
            global serial
            a = serial.read(20)
            task = {"info" : a}
            return task
        except BaseException as e:
           del serial
    def delete(self, todo_id):
        del TODOS[todo_id]
        return '', 204
    def put(self, todo_id):
        args = parser.parse_args()
        task = {'task': args['task']}
        TODOS[todo_id] = task
        return task, 201
    def post(self,todo_id):
        args = parser.parse_args()
        a = request.form.get('motion')
        print(a)
        if a == "start":
		    global serial
            serial.write(TODOS['todo1'])
            time.sleep(0.1)
        task = {"states":"ok"}
        return task, 201	
class 
api.add_resource(holder_Control, '/todos/')  #添加资源
#api.add_resource(holder_Control, '/todos/')  #添加资源
#api.add_resource(location, '/todos/')  #添加资源
#api.add_resource(motorControl, '/todos/')  #添加资源
#api.add_resource(state_info, '/todos/')  #添加资源
if __name__ == '__main__':
    app.run(debug=True) #app.run(host='0.0.0.0',port=5000,debug=True)部署到树莓派上时使用这条指令

 

你可能感兴趣的:(web开发)