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. DNSMaper 一款子域名枚举与地图标记工具

    DNSMaper DNSMaper拥有与众多子域名枚举工具相似的功能,诸如域传送漏洞检测,子域名枚举,IP地址获取 文件说明├── dnsmaper.py(核心代码)├── dnsmapper.png ...

  2. SuperMemo method

    原文:https://www.supermemo.com/en/archives1990-2015/english/ol/sm2 别人的研究:http://wdxtub.lofter.com/post ...

  3. 好用的redis客户端和Mongo客户端推荐

    Another Redis resktop manager electron页面 github地址 MongoCompass 社区版 下载地址

  4. PHP通过iconv将字符串从GBK转换为UTF8字符集

    PHP通过iconv将字符串从GBK转换为UTF8字符集的方法,需要的朋友可以参考下. 1. iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件.例如:从GB23 ...

  5. IP分组

    IP 分组为了更准确地讨论 I n t e r n e t协议处理,我们必须定义一些名词.图 显示了在不同的I n t e r n e t层之间传递数据时用来描述数据的名词.我们把传输协议交给 I P ...

  6. 图片转换成十六进制TXT文件

    最近学习了图片的转换,就学习了一下图片从二进制转换成十六进制,十六进制TXT文件转换成图片形式. using System; using System.Collections.Generic; usi ...

  7. java web项目中后台控制层对参数进行自定义验证 类 Pattern

    Pattern pattern = Pattern.compile("/^([1-9]\d+元*|[0]{0,1})$/");//将给定的正则表达式编译到模式中 if(!" ...

  8. Python中的字典分组函数(groupby,itertools)

    from operator import itemgetter # itemgetter用来去dict中的key,省去了使用lambda函数 from itertools import groupby ...

  9. 「数据结构与算法(Python)」(一)

    算法的提出 算法的概念 算法是计算机处理信息的本质,因为计算机程序本质上是一个算法来告诉计算机确切的步骤来执行一个指定的任务.一般地,当算法在处理信息时,会从输入设备或数据的存储地址读取数据,把结果写 ...

  10. IAR添加debug和release选项

    在IAR的Workspace窗口顶部的下拉菜单中有两个选项,Debug和Release. 名字和数量可以在菜单栏的Project-->Edit Configuration中增删修改 每个选项都对 ...