一 flask 介绍

发布时间:2020-06-11 18:52:41 作者:wx584f813adcf41
来源:网络 阅读:353

flask是一款micro web 服务器

介绍

轻量级服务器是指服务器剔除了一些功能,这样服务器在部署,运行时开销就会降低,变得很轻。
这里的micro web 服务器是指flask为了保持core简单,但是功能是可以扩展的。也就是说flask只提供了核心的web server 功能。
flask没有提供database abstraction layer, form validation, upload handling等其它功能,但是却提供了扩展来支持这些功能。

1 开始Hello, World!

code

from flask import Flask

app = Flask(name)
#Flask 实例实现了WSGI(Web Server Gateway Interface)接口,
name作为参数是为了查找templates, static files等等。更多的信息参考flask文档。

@app.route('/')
def hello_world():
return 'Hello, World!'
#使用route decorator使用指定的URL来触发function

run server

set FLASK_APP=01_hello.py
set FLASK_ENV=development
开启debug模式
flask run --host=0.0.0.0
指定server在所有public IPs监听

browser

http://127.0.0.1:5000/

2 Routing

使用 route() decorator 绑定a function 到 a URL.@app.route('/')
br/>@app.route('/')
return 'Hello, World!'

3 Variable Rules

位URL指定参数,可选地还可以指定类型。最后传给函数。

@app.route('/user/<username>')
def show_user_profile(username):
return 'User %s' % (username,)

@app.route('/post/<int:post_id>')
def show_post(post_id):
return 'post %d' % (post_id,)

案例:@app.route('/user/<username>')
br/>@app.route('/user/<username>')
def show_user_profile(username):
return 'User %s' % (username,)

@app.route('/post/<int:post_id>')
def show_user_profile(post_id):
return 'post %d' % (post_id,)
报错:
AssertionError: View function mapping is overwriting an existing endpoint function: show_user_profile
原因:
两个URL绑定到一个view function, flask认为我们队view function进行了overwriting

4 Unique URLs / Redirection Behavior

@app.route('/projects/')
如果请求URL为/projects,这是会redirect到/projects/
请求会响应一个308,然后再响应一个200

note: URL 匹配规则为由上向下

案例:@app.route('/post/<int:post_id>/')
br/>@app.route('/post/<int:post_id>/')
return 'post %d' % (post_id,)

@app.route('/post/<int:post_id>')
def show_post_profile(post_id):
return 'post profile %d' % (post_id,)

结果:
不管请求是post/2 还是post/2/,只有show_post响应。

方案: 调换两个view function的顺序。

5 URL Building

建立一个对应function的URL
用法:
第一个参数: function name
后面的key arguments对应URL variables
未知的arguments对应URL query variables

案例:
with app.test_request_context():
print(url_for('index'))
print(url_for('show_user_profile',username='test'))
print(url_for('show_user_profile',username='test',key='value'))
输出:
/
/user/test
/user/test?key=value

6 HTTP Methods

处理不同的请求,需要为route()指定methods参数

案例:@app.route('/login')
br/>@app.route('/login')
return render_template('login.html')
请求:
http://127.0.0.1:5000/login
输出:
405 Method Not Allowed
原因:
@app.route('/login') 默认只处理GET请求,不处理POST请求

7 Rendering Templates

使用render_template函数render a template
第一个参数:html template
后面的key arguments,为传递给template的变量

上下文传递:
server -> template
browser form, URL variables -> server
redirect(URL Building) --> browser

案例:
def index():
user = ''
if request.method == 'POST':
user = request.form['user']
return render_template('index.html',user=user)
else:
return render_template('index.html',user=user)

<!doctype html>
<title> Hello from Flask</title>
<% if user %>
<h2> Hello {{ user }}!</h2>
<% else %>
<h2> index page!</h2>
<% endif %>

8 Accessing Request Data

context locals

9 The Request Objec

通过全局变量request获取browser传递的消息
例如:
form:
request.form['username']
parameters:
searchword = request.args.get('key', '')
cooks:
headers:

推荐阅读:
  1. flask学习之数据库操作一
  2. 一、flask的基本使用-flask

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

zabbix flask la

上一篇:利用Loadrunner来监控Linux服务器

下一篇:Python装饰器函数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》