博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Flask开发
阅读量:6711 次
发布时间:2019-06-25

本文共 2444 字,大约阅读时间需要 8 分钟。

  hot3.png

python编码:

#!flask/bin/python#cmd /k python "$(FULL_CURRENT_PATH)" & ECHO. & PAUSE & EXIT#pip install flask-httpauth jsonify pyOpenSSLfrom flask import Flask, jsonify, abort, make_response, requestfrom flask.ext.httpauth import HTTPBasicAuthauth = HTTPBasicAuth()@auth.get_passworddef get_password(username):    if username == 'ok':        return 'python'    return None@auth.error_handlerdef unauthorized():    return make_response(jsonify({'error': 'Unauthorized access'}), 401)app = Flask(__name__)#app.run('localhost', debug=True, port=8100, ssl_context=('server.crt', 'server.key')) #app.run('localhost', debug=True, port=8100, ssl_context='adhoc')@app.route('/index')def index():    return "Hello, World!"    @app.route('/')def root():    return "Hello, World!"    tasks = [    {        'id': 1,        'title': u'Buy groceries',        'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',         'done': False    },    {        'id': 2,        'title': u'Learn Python',        'description': u'Need to find a good Python tutorial on the web',         'done': False    }]@app.errorhandler(404)def not_found(error):    return make_response(jsonify({'error': 'Not found'}), 404)#http://localhost:5000/todo/api/v1.0/tasks@app.route('/todo/api/v1.0/tasks', methods=['GET'])@auth.login_requireddef get_tasks():    return jsonify({'tasks': tasks})    @app.route('/todo/api/v1.0/tasks/
', methods=['GET'])@auth.login_requireddef get_task(task_id): task = filter(lambda t: t['id'] == task_id, tasks) if len(task) == 0: abort(404) return jsonify({'task': task[0]}) #curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://localhost:5000/todo/api/v1.0/tasks@app.route('/todo/api/v1.0/tasks', methods=['POST'])def create_task(): if not request.json or not 'title' in request.json: abort(400) task = { 'id': tasks[-1]['id'] + 1, 'title': request.json['title'], 'description': request.json.get('description', ""), 'done': False } tasks.append(task) return jsonify({'task': task}), 201 if __name__ == '__main__': app.run('localhost', debug=True, port=5000)#, ssl_context='adhoc')

 

参考:

http://www.cnblogs.com/vovlie/p/4178077.html

原URL:https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask

http://blog.csdn.net/shenzhan168/article/details/47783651

转载于:https://my.oschina.net/sanpeterguo/blog/872350

你可能感兴趣的文章
PhalApi-APK--APK文件解包处理
查看>>
OSChina 周日乱弹 ——小明毕业后,到银行上班…
查看>>
OSChina 周日乱弹 ——手上在说不,嘴巴很诚实
查看>>
hbase0.98 coprocessor Endpoint 之 HelloWorld
查看>>
jquery中form表单/prop/attr/width/height/offset/scroll的注意事项
查看>>
swift 纯代码tableView
查看>>
给注解打断点的一种方法
查看>>
AVM2-as3.0的运行机制,靠实践摸索的
查看>>
CentOS7:搭建SVN + Apache 服务器
查看>>
css的#和.的区别
查看>>
9.(1.0.2更新)用户队列管理 与 用户异常处理
查看>>
linux shell 获取字符串md5值
查看>>
Object-C代码练习【谓词】
查看>>
storm与spring结合开发
查看>>
使用Golang实现网页爬虫
查看>>
树莓派查看WIFI链接配置文件
查看>>
laradock win10安装
查看>>
ubuntu System Program Problem Detected解决
查看>>
在Linux下用Wake On LAN实现远程开机
查看>>
启动jar命令
查看>>