获取请求报文中传递的数据
示例
请求网址:http://127.0.0.1:8000/req/?a=1&b=2&c=3
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> request
<span class="hljs-meta">@app.route('/req/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">req</span><span class="hljs-params">()</span>:</span>
<span class="hljs-comment"># 完整的请求URL</span>
<span class="hljs-comment"># return request.url</span>
<span class="hljs-comment"># 基本路由地址,不包括get参数</span>
<span class="hljs-comment"># return request.base_url</span>
<span class="hljs-comment"># 只有主机和端口号</span>
<span class="hljs-comment"># return request.host_url</span>
<span class="hljs-comment"># 只包含装饰器中的路由地址</span>
<span class="hljs-comment"># return request.path</span>
<span class="hljs-comment"># 去掉了ip和端口剩下的完整的url</span>
<span class="hljs-comment"># return request.full_path</span>
<span class="hljs-comment"># 请求方法类型</span>
<span class="hljs-comment"># return request.method</span>
<span class="hljs-comment"># 客户端的IP</span>
<span class="hljs-comment"># return request.remote_addr</span>
<span class="hljs-comment"># 获取GET参数</span>
<span class="hljs-comment"># return request.args</span>
<span class="hljs-comment"># 提取url中的部分值传递给视图的参数</span>
<span class="hljs-comment"># return request.view_args</span>
<span class="hljs-comment"># 存储所有请求的cookie</span>
<span class="hljs-comment"># return request.cookies</span>
<span class="hljs-comment"># 获取请求头信息</span>
<span class="hljs-keyword">return</span> request.headers[<span class="hljs-string">'User-Agent'</span>]
get 传参使用
url:http://127.0.0.1:5000/args/?a=1&b=2&c=3&c=4
<span class="hljs-meta">@myApp.route("/args/")</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">args</span><span class="hljs-params">()</span>:</span>
a = request.args.get(<span class="hljs-string">"a"</span>)
b = request.args.get(<span class="hljs-string">"b"</span>)
clist = request.args.getlist(<span class="hljs-string">"c"</span>)
print(a, b, clist)
<span class="hljs-keyword">return</span> <span class="hljs-string">"获取到了get参数"</span>
获取POST传参
test_post.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/post/" method="POST">
<p>用户名:<input type="text" name="username" placeholder="输入用户名"></p>
<p>年龄:<input type="text" name="age" placeholder="输入年龄"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
视图函数
<span class="hljs-meta">@myApp.route("/post/", methods=["POST", "GET"])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_post</span><span class="hljs-params">()</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">'POST'</span>
username = request.form.get(<span class="hljs-string">"username"</span>)
age = request.form.get(<span class="hljs-string">"age"</span>)
print(username, age)
<span class="hljs-keyword">return</span> username, age
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> render_template(<span class="hljs-string">'test_post.html'</span>)