Python的Flask开发框架简单上手笔记


Posted in Python onNovember 16, 2015

最简单的hello world

#!/usr/bin/env python
# encoding: utf-8

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
  return 'hello world'

if __name__ == '__main__':
  app.run(debug=True)
  #app.run(host='127.0.0.1', port=8000)

之后,访问http://localhost:5000

支持post/get提交

@app.route('/', methods=['GET', 'POST'])

多个url指向

@app.route('/')
@app.route('/index')

不管post/get使用统一的接收

from flask import request
args = request.args if request.method == 'GET' else request.form
a = args.get('a', 'default')

处理json请求
request的header中

"Content-Type": "application/json"

处理时:

data = request.get_json(silent=False)

获取post提交中的checkbox

{%for page in pages %}
<tr><td><input type=checkbox name=do_delete value="{{ page['id'] }}"></td><td>
{%endfor%}

page_ids = request.form.getlist("do_delete")

使用url中的参数

@app.route('/query/<qid>/')
def query(qid):
  pass

在request开始结束dosomething
一般可以处理数据库连接等等

from flask import g

app = .....

@app.before_request
def before_request():
  g.session = create_session()

@app.teardown_request
def teardown_request(exception):
  g.session.close()

注册Jinja2模板中使用的过滤器

@app.template_filter('reverse')
def reverse_filter(s):
  return s[::-1]

或者

def reverse_filter(s):
  return s[::-1]
app.jinja_env.filters['reverse'] = reverse_filter

可以这么用

def a():...
def b():...

FIL = {'a': a, 'b':b}
app.jinja_env.filters.update(FIL)

注册Jinja2模板中使用的全局变量

JINJA2_GLOBALS = {'MEDIA_PREFIX': '/media/'}
app.jinja_env.globals.update(JINJA2_GLOBALS)

定义应用使用的template和static目录

app = Flask(__name__, template_folder=settings.TEMPLATE_FOLDER, static_folder = settings.STATIC_PATH)

使用Blueprint

from flask import Blueprint
bp_test = Blueprint('test', __name__)
#bp_test = Blueprint('test', __name__, url_prefix='/abc')

@bp_test.route('/')

--------
from xxx import bp_test

app = Flask(__name__)
app.register_blueprint(bp_test)

实例:

bp_video = Blueprint('video', __name__, url_prefix='/kw_news/video')
@bp_video.route('/search/category/', methods=['POST', 'GET'])
#注意这种情况下Blueprint中url_prefix不能以 '/' 结尾, 否则404

使用session
包装cookie实现的,没有session id

app.secret_key = 'PS#yio`%_!((f_or(%)))s'

然后

from flask import session

session['somekey'] = 1
session.pop('logged_in', None)

session.clear()

#过期时间,通过cookie实现的
from datetime import timedelta
session.permanent = True
app.permanent_session_lifetime = timedelta(minutes=5)

反向路由

from flask import url_for, render_template

@app.route("/")
def home():
  login_uri = url_for("login", next=url_for("home"))
  return render_template("home.html", **locals())

上传文件

<form action="/image/upload/" method="post" enctype="multipart/form-data">
<input type="file" name="upload" />

接收

f = request.files.get('upload')
img_data = f.read()

直接返回某个文件

return send_file(settings.TEMPLATE_FOLDER + 'tweet/tweet_list.html')

请求重定向

flask.redirect(location, code=302) the redirect status code. defaults to 302.Supported codes are 301, 302, 303, 305, and 307. 300 is not supported.

@app.route('/')
def hello():
  return redirect(url_for('foo'))

@app.route('/foo')
def foo():
  return'Hello Foo!'

获取用户真实ip
从request.headers获取

real_ip = request.headers.get('X-Real-Ip', request.remote_addr)
或者, 使用werkzeug的middleware 文档

from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
return json & jsonp
import json
from flask import jsonify, Response, json

data = [] # or others
return jsonify(ok=True, data=data)

jsonp_callback = request.args.get('callback', '')
if jsonp_callback:
  return Response(
      "%s(%s);" % (jsonp_callback, json.dumps({'ok': True, 'data':data})),
      mimetype="text/javascript"
      )
return ok_jsonify(data)

配置读取方法

# create our little application :)
app = Flask(__name__)

# Load default config and override config from an environment variable
app.config.update(dict(
  DATABASE='/tmp/flaskr.db',
  DEBUG=True,
  SECRET_KEY='development key',
  USERNAME='admin',
  PASSWORD='default'
))
app.config.from_envvar('FLASKR_SETTINGS', silent=True)


------------------
# configuration
DATABASE = '/tmp/minitwit.db'
PER_PAGE = 30
DEBUG = True
SECRET_KEY = 'development key'

# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('MINITWIT_SETTINGS', silent=True)

几个不常用的方法

from flask import abort, flash

abort
if not session.get('logged_in'):
  abort(401)

flash
flash('New entry was successfully posted')

异步调用
想在flask的一个请求中处理异步, 除了使用消息系统, 可以用简单的线程处理

from threading import Thread

def async(f):
  def wrapper(*args, **kwargs):
    thr = Thread(target=f, args=args, kwargs=kwargs)
    thr.start()
  return wrapper

@async
def dosomething(call_args):
  print call_args


in a request handler, call `dosomething`
error handler
@app.errorhandler(404)
def not_found_error(error):
  return render_template('404.html'), 404

@app.errorhandler(500)
def internal_error(error):
  db.session.rollback()
  return render_template('500.html'), 500

项目配置
1.直接

app.config['HOST']='xxx.a.com'
print app.config.get('HOST')

2.环境变量

export MyAppConfig=/path/to/settings.cfg
app.config.from_envvar('MyAppConfig')

3.对象

class Config(object):
   DEBUG = False
   TESTING = False
   DATABASE_URI = 'sqlite://:memory:'

 class ProductionConfig(Config):
   DATABASE_URI = 'mysql://user@localhost/foo'

 app.config.from_object(ProductionConfig)
 print app.config.get('DATABASE_URI') # mysql://user@localhost/foo

4.文件

# default_config.py
HOST = 'localhost'
PORT = 5000
DEBUG = True

app.config.from_pyfile('default_config.py')

EG. 一个create_app方法

from flask import Flask, g

def create_app(debug=settings.DEBUG):
  app = Flask(__name__,
        template_folder=settings.TEMPLATE_FOLDER,
        static_folder=settings.STATIC_FOLDER)

  app.register_blueprint(bp_test)

  app.jinja_env.globals.update(JINJA2_GLOBALS)
  app.jinja_env.filters.update(JINJA2_FILTERS)

  app.secret_key = 'PO+_)(*&678OUIJKKO#%_!(((%)))'

  @app.before_request
  def before_request():
    g.xxx = ...  #do some thing

  @app.teardown_request
  def teardown_request(exception):
    g.xxx = ...  #do some thing

  return app

app = create_app(settings.DEBUG)
host=settings.SERVER_IP
port=settings.SERVER_PORT
app.run(host=host, port=port)
change log:

2013-09-09 create
2014-10-25 update
Python 相关文章推荐
探究python中open函数的使用
Mar 01 Python
Python编码爬坑指南(必看)
Jun 10 Python
Python生成随机数组的方法小结
Apr 15 Python
Python 高级专用类方法的实例详解
Sep 11 Python
python中使用正则表达式的连接符示例代码
Oct 10 Python
Python Flask前后端Ajax交互的方法示例
Jul 31 Python
Python多进程与服务器并发原理及用法实例分析
Aug 21 Python
对python3 中方法各种参数和返回值详解
Dec 15 Python
基于Python的Post请求数据爬取的方法详解
Jun 14 Python
python中append函数用法讲解
Dec 11 Python
详解用selenium来下载小姐姐图片并保存
Jan 26 Python
Python Django / Flask如何使用Elasticsearch
Apr 19 Python
python实现mysql的单引号字符串过滤方法
Nov 14 #Python
浅析Python中signal包的使用
Nov 13 #Python
Python下rrdtool模块的基本使用方法
Nov 13 #Python
简单了解Python下用于监视文件系统的pyinotify包
Nov 13 #Python
Python的pycurl包用法简介
Nov 13 #Python
使用Python下载歌词并嵌入歌曲文件中的实现代码
Nov 13 #Python
Python设置Socket代理及实现远程摄像头控制的例子
Nov 13 #Python
You might like
php的一个登录的类 [推荐]
2007/03/16 PHP
php include,include_once,require,require_once
2008/09/05 PHP
简单的php写入数据库类代码分享
2011/07/26 PHP
php操作(删除,提取,增加)zip文件方法详解
2015/03/12 PHP
基于jquery的一个图片hover的插件
2010/04/24 Javascript
JavaScript中URL编码函数代码
2011/01/11 Javascript
js定义对象或数组直接量时各浏览器对多余逗号的处理(json)
2011/03/05 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
探究JavaScript函数式编程的乐趣
2015/12/14 Javascript
实例讲解jquery中mouseleave和mouseout的区别
2016/02/17 Javascript
vue-prop父组件向子组件进行传值的方法
2018/03/01 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
node实现socket链接与GPRS进行通信的方法
2019/05/20 Javascript
基于vue+axios+lrz.js微信端图片压缩上传方法
2019/06/25 Javascript
Vue初始化中的选项合并之initInternalComponent详解
2020/06/11 Javascript
解决vue-loader加载不上的问题
2020/10/21 Javascript
[42:32]完美世界DOTA2联赛循环赛 Magma vs PXG BO2第二场 10.28
2020/10/28 DOTA
利用Python生成文件md5校验值函数的方法
2017/01/10 Python
Collatz 序列、逗号代码、字符图网格实例
2017/06/22 Python
Python使用progressbar模块实现的显示进度条功能
2018/05/31 Python
基于python的socket实现单机五子棋到双人对战
2020/03/24 Python
Python 占位符的使用方法详解
2019/07/10 Python
Pytest测试框架基本使用方法详解
2020/11/25 Python
阿迪达斯俄罗斯官方商城:adidas俄罗斯
2017/03/08 全球购物
GE设备配件:GE Appliance Parts(家电零件、配件和滤水器)
2018/11/28 全球购物
Dogeared官网:在美国手工制作的珠宝
2019/08/24 全球购物
如何唤起类中的一个方法
2013/11/29 面试题
机械工程师求职自我评价
2013/09/23 职场文书
开会迟到检讨书
2014/02/03 职场文书
中学教师自我鉴定
2014/02/07 职场文书
小学德育工作经验交流材料
2014/05/22 职场文书
迎新晚会策划方案
2014/06/13 职场文书
护士先进个人总结
2015/02/13 职场文书
和领导吃饭祝酒词
2015/08/11 职场文书
python实现语音常用度量方法的代码详解
2021/05/25 Python
MySQL 分区表中分区键为什么必须是主键的一部分
2022/03/17 MySQL