python flask demo
from flask import Flask, jsonify
from flask import abort
from flask import make_response
from flask import request
from flask_httpauth import HTTPBasicAuth
from flask import url_for
auth = HTTPBasicAuth()
app = Flask(__name__) def make_public_task(task):
new_task = {}
for field in task:
if field == 'id':
new_task['uri'] = url_for('get_task', task_id=task['id'], _external=True)
else:
new_task[field] = task[field]
return new_task
tasks = [
{
'id': ,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': ,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/')
def index():
return "Hello, World!" @app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks}) @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = list(filter(lambda t: t['id'] == task_id, tasks))
if len(task) == :
abort()
return jsonify({'task': task[]}) @app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort()
task = {
'id': tasks[-]['id'] + ,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': tasks}), @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
task = list(filter(lambda t: t['id'] == task_id, tasks))
if len(task) == :
abort()
if not request.json:
abort()
print(type(request.json['title']))
if 'title' in request.json and type(request.json['title']) != unicode:
abort()
if 'description' in request.json and type(request.json['description']) is not unicode:
abort()
if 'done' in request.json and type(request.json['done']) is not bool:
abort()
task[]['title'] = request.json.get('title', task[]['title'])
task[]['description'] = request.json.get('description', task[]['description'])
task[]['done'] = request.json.get('done', task[]['done'])
return jsonify({'task': task[]}) @app.route('/todo/api/v1.0/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
task = list(filter(lambda t: t['id'] == task_id, tasks))
if len(task) == :
abort()
tasks.remove(task[])
return jsonify({'result': True}) @app.errorhandler()
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), ) @auth.get_password
def get_password(username):
if username == 'ok':
return 'python'
return None
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), )
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), )
if __name__ == '__main__':
app.run(debug=True)
python flask demo的更多相关文章
- python学习笔记十三:Flask demo
一.Flask简介 Flask 是一个 Python 实现的 Web 开发微框架.官网:http://flask.pocoo.org/ 二.Demo 1.代码结构 . ├── blog.py ├── ...
- Python Flask学习笔记之Hello World
Python Flask学习笔记之Hello World 安装virtualenv,配置Flask开发环境 virtualenv 虚拟环境是Python解释器的一个私有副本,在这个环境中可以安装私有包 ...
- Python Flask 实现移动端应用接口(API)
引言 目前,Web 应用已形成一种趋势:业务逻辑被越来越多地移到客户端,逐渐完善为一种称为富互联网应用(RIA,rich Internet application)的架构.在 RIA 中,服务器的主要 ...
- 实战接口开发:python + flask + mysql + redis(根据反馈,持续细化更新。。。)
前言 自动化已经成为测试的必备技能之一了,所以,很多想跳槽的测试朋友都在自学,特别是最实用的接口自动化, 但是很多人因为没有可以练手的项目而苦恼,最终导致缺乏实战经验,其实,完全可以自己开发个简单项目 ...
- Python+Flask+MysqL的web技术建站过程
1.个人学期总结 时间过得飞快,转眼间2017年就要过去.这一年,我学习JSP和Python,哪一门都像一样新的东西,之前从来没有学习过. 这里我就用我学习过的Python和大家分享一下,我是怎么从一 ...
- Python Flask框架路由简单实现
Python Flask框架路由的简单实现 也许你听说过Flask框架.也许你也使用过,也使用的非常好.但是当你在浏览器上输入一串路由地址,跳转至你所写的页面,在Flask中是怎样实现的,你是否感到好 ...
- 初探 Python Flask+Jinja2 SSTI
初探 Python Flask+Jinja2 SSTI 文章首发安全客:https://www.anquanke.com/post/id/226900 SSTI简介 SSTI主要是因为某些语言的框架中 ...
- [python][flask] Flask 图片上传与下载例子(支持漂亮的拖拽上传)
目录 1.效果预览 2.新增逻辑概览 3.tuchuang.py 逻辑介绍 3.1 图片上传 3.2 图片合法检查 3.3 图片下载 4.__init__.py 逻辑介绍 5.upload.html ...
- AFNetworking+Python+Flask+pyOpenSSL构建iOS HTTPS客户端&服务器端
对于HTTPS我在网上找了一堆资料看了下, 各种协议和证书已经有点晕了 最后我现有的感觉是, 在HTTP服务器上放一个证书, 在原本的HTTP访问之前客户端先检查证书是否正确 如果客户端证书检查正确, ...
随机推荐
- Word转换为markdown
Word转换为markdown 首先你的电脑要有office word 1 安装pandoc https://github.com/jgm/pandoc/releases,可以找到最新的pando ...
- window下安装mongodb3.6
系统:Win10 x64位 1.在官网下载对应的mongod https://www.mongodb.com/download-center?jmp=nav#community 2.下载后在win+R ...
- windows7触屏编程
每当用户触摸触敏式 Windows 7 设备时,Windows 7 多点触控平台都会向您的应用程序发送手势消息 WM_GESTURE.这是现成的免费行为,如果您希望停止接收此类消息,则需要选择退出. ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- oracle怎么恢复被覆盖的存储过程
在oracle数据库中,如果覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小. 原理很简单,存储过程的定义就是数据字典,修改数据字典跟修改普通表的数据没有区别,此时会把修改前的内容放到un ...
- Look for the Air Jordan 32 in full family sizing
Following the release of the 'Rosso Corsa' colorway, Jordan Brand is now set to officially launch th ...
- Twitter OA prepare: Anagram is A Palindrome
Algorithm: Count the number of occurrence of each character. Only one character with odd occurrence ...
- liferay项目经验之BasePortlet
package com.ebizwindow.crm.portlet.base; import java.io.IOException; import java.io.InputStream; imp ...
- excel输入数字变成特殊符号问题
问题,在单元格里输入数字,结果变成文件夹类型的小图片或特殊符号了,原因是字体为Wingdings,将其设为Times New Roman即可
- 浅谈css中渐变衔接
无论transition还是keyframes,如何让变化更自然,这是前端应该考虑的问题. 这里,我简单总结下自己的方法. 以实践为例子. 1.图像渐变 @keyframes looppic{ fro ...