Flask request 属性详解

一、关于request
在Flask的官方文档中是这样介绍request的:对于 Web 应用,与客户端发送给服务器的数据交互至关重要。在 Flask 中由全局的 request 对象来提供这些信息。

从Flask模块导入request:from flask import request
request的属性:下面是request可使用的属性,其中黑体是比较常用的。

 

二、常用方法的使用

#代码示例,仅仅是为了测试request的属性值
@app.route('/login', methods = ['GET','POST'])
def login():
if request.method == 'POST':
if request.form['username'] == request.form['password']:
return 'TRUE'
else:
#当form中的两个字段内容不一致时,返回我们所需要的测试信息
return str(request.headers) #需要替换的部分
else:
return render_template('login.html')

1、method:请求的方法

return request.method        #POST

2、form:返回form的内容

return json.dumps(request.form)        #{"username": "123", "password": "1234"}

3、args和values:args返回请求中的参数,values返回请求中的参数和form

return json.dumps(request.args)
#url:http://192.168.1.183:5000/login?a=1&b=2、返回值:{"a": "1", "b": "2"}
print(request.args['a'])
#输出:1
return str(request.values)
#CombinedMultiDict([ImmutableMultiDict([('a', '1'), ('b', '2')]), ImmutableMultiDict([('username', '123'), ('password', '1234')])])

4、cookies:cookies信息

return str(request.headers)        #headers信息
request.headers.get('User-Agent') #获取User-Agent信息

6、url、path、script_root、base_url、url_root:看结果比较直观

return 'url: %s , script_root: %s , path: %s , base_url: %s , url_root : %s' % (request.url,request.script_root, request.path,request.base_url,request.url_root)
'''
url: http://192.168.1.183:5000/testrequest?a&b ,
script_root: ,
path: /testrequest ,
base_url: http://192.168.1.183:5000/testrequest ,
url_root : http://192.168.1.183:5000/
'''

7、date、files:date是请求的数据,files随请求上传的文件

@app.route('/upload',methods=['GET','POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
filename = secure_filename(f.filename)
#f.save(os.path.join('app/static',filename))
f.save('app/static/'+str(filename))
return 'ok'
else:
return render_template('upload.html') #html
<!DOCTYPE html>
<html>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" /><br />
<input type="submit" value="Upload" />
</form>
</body>
</html>

Flask request 属性详解的更多相关文章

  1. request.getcontextPath() 详解

    request.getcontextPath() 详解 文章分类:Java编程 <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但 ...

  2. OutputCache属性详解(三)— VaryByHeader,VaryByCustom

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  3. django中request对象详解(转载)

    django中的request对象详解 Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将  HttpRequest对象  作为第一个参数传入该函数. ...

  4. fiddler响应报文的headers属性详解

    fiddler响应报文的headers属性详解 (1)Cache头域 1. Cache-Control 在请求报文已经说过了,用于设置缓存的属性,浏览内容不被缓存. 2. Data 生成消息的具体时间 ...

  5. fiddler请求报文的headers属性详解

    fiddler请求报文的headers属性详解 headers的属性包含以下几部分. (1)Cache头域 在Cache头域中,通常会出现以下属性. 1. Cache-Control 用来指定Resp ...

  6. tomcat 三种部署方式以及server.xml文件的几个属性详解

    一.直接将web项目文件件拷贝到webapps目录中 这是最常用的方式,Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.如果你想要修改这个默认 ...

  7. android:exported 属性详解

    属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...

  8. OutputCache属性详解(一)一Duration、VaryByParam

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  9. OutputCache属性详解(二)一 Location

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

随机推荐

  1. C# 普通的辅助类

    在数字前面补0 /// <summary> /// 在数字前面添加0 /// </summary> /// <param name="num"> ...

  2. mac使用sourcetree跳过注册

    转自https://blog.csdn.net/qq_32890891/article/details/89216954 打开sourcetree 关闭sourcetree 命令终端输入default ...

  3. nginx 重发机制导致的重复扣款问题

    问题:    nginx 重发机制导制重复提交(客户还款,被扣俩笔款,前端调用一次,后端执行2次) proxy_next_upstream 语法: proxy_next_upstream error ...

  4. webpack4 Cannot find module '@babel/core'

    Error: // webpackCannot find module '@babel/core'解决办法一: 原因"babel-loader": "^8.0.0&quo ...

  5. TreadPool

    ThreadPool概述 提供一个线程池,该线程池可用于执行任务.发送工作项.处理异步 I/O.代表其他线程等待以及处理计时器. 创建线程需要时间.如果有不同的小任务要完成,就可以事先创建许多线程/在 ...

  6. python yaml文件内容的读取

    示例: (1)host_header.yaml  文件中的内容 host: https://testapp.goodiber.com/v2/ #dev1的测试环境域名 #请求接口的请求头中的共用参数 ...

  7. 一步一步学会preload和prefetch

    preload和prefetch是什么? 我们常说的preload和prefetch,是link标签rel里新增的两种值,用于让浏览器提前加载指定的资源,它们会先被缓存(属于http cache缓存) ...

  8. python的logging日志模块(一)

    最近修改了项目里的logging相关功能,用到了Python标准库里的logging模块,在此做一些记录.主要是从官方文档和stackoverflow上查询到的一些内容. 官方文档 技术博客 基本用法 ...

  9. python原生操作mysql

    import pymysql HOST = '127.0.0.1' PORT = 3306 USER = 'root' PASSWD = ' DB = 'test' CHARSET = 'utf8' ...

  10. 006_linux驱动之_ioremap函数使用

    (一)学习linux驱动之初,对ioremap函数的个人理解 (二)博客:实验探究 ioremap 这篇文章作者通过验证来阐述自己的观点,个人觉得挺好的 (三)函数原型 基本简介 void * __i ...