flask与http
-
URL组成
http://www.example.com/hello?name=Jerry#comment协议: http
主机:www.example.com
请求资源path: /hello
查询字符串:name=Jerry
锚点: #comment -
请求报文
包括 请求方法,URL, 协议,头部信息,请求内容
请求方法
请求 | 说明 |
---|---|
PUT | 获取资源 |
POST | 创建或更新资源 |
PUT | 创建或替换资源 |
DELETE | 删除资源 |
HEAD | 获取头部信息 |
OPTIONS | 询问支持的方法 |
Requst对象
属性 | 值 |
---|---|
path | ‘/hello’ |
full_path | ‘/hello?name=Jerry’ |
host | ‘www.example.com’ |
host_url | ‘/http://www.example.com’ |
base_url | ‘http://www.example.com/hello’ |
url | ‘http://www.example.com/hello?name=Jerry’ |
form | 获取表单提交数据 |
args | 获取查询字符串 |
files | 获取上传文件 |
- 请求处理
- 路由匹配
@app.route(’/hello’) - HTTP方法
@app.route(’/hello’, methods=[‘GET’, ‘POST’]) - url处理
@app.route(’/hello/<int:year>’)
- http响应
- 响应报文
协议版本, 状态码,状态码, 原因短语, 响应头, 响应主体 - flask响应对象
def index():
res = make_response('首页', 200)
res.headers['Content-type'] = 'text/plain;charset=utf-8'
# res.mimetype = 'text/plain'
return res
方法 | 说明 |
---|---|
headers | 头部信息 |
status | 状态码, 文本 |
status_code | 状态码 |
mimetype | MIME类型 |
set_cookie() | 设置一个cookie |
- Cookie
res.set_cookie(‘key’, ‘value’, ‘domain’, ‘path’, expires=datetime.now() + timedelta(minutes=10), httponly)
读取
request.cookies.get(‘name’, ‘None’)