【Sanic框架】使用Python异步非阻塞框架Sanic进行数据请求 request对象(二)

在上一篇的Sanic系列教程【使用Python异步非阻塞框架Sanic搭建一个简单的Sanic web应用】介绍建议Demo:

from sanic import Sanic
from sanic.response import json

app = Sanic(__name__)

@app.route('/')
async def testDemo(request):
    msg = {'message': 'Welcom to Python'}
    return json(msg, ensure_ascii=False)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8888)

路由函数testDemo()有一个参数request,它是一个Request对象,包含了客户端(浏览器)发过来的HTTP请求的各类数据。 request对象是web应用要处理的对象,它包含了客户端(浏览器)的请求数据,通过它的各种属性来访问这些请求数据。

本节教程我们来看一下 Sanic Request对象的属性:

from sanic import Sanic
from sanic import response
from sanic.response import json

app = Sanic(__name__)

@app.route('/')
async def test(request):
    msg = {'message': 'Welcom to Python'}
    return json(msg, ensure_ascii=False)


# 1、请求参数json: JSON格式数据 当客户端POST来的数据是json格式时,可以通过request.json来访问
@app.route('/json', methods=['POST'])
async def post_json(request):
    return response.json({"received": request.json})


# 2、请求参数args: 查询解析后的字符串变量(字典)
# 3、请求参数url: 请求路径(返回字符串变量)
# 4、请求参数query_string: 未解析字符串变量(字符串)
@app.route('/args')
async def args(request):

    return response.json({
        "parsed": True,
        "args": request.args,
        "url": request.url,
        "query_string": request.query_string,
        "socket": request.socket,
        "scheme": request.scheme,
        "host": request.host,
        "path": request.path,
        "uri_template": request.uri_template,
        "token": request.token,
        "form": request.form,
        "method": request.method,
        "files": request.files,
        "id": request.ip,
        "port": request.port,
    })
   
if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8888)

在这里插入图片描述

Sanic Request对象的属性查询:

1、json :JSON格式数据

当客户端POST来的数据是json格式时,可以通过request.json来访问:

from sanic import response

@app.route('/json', methods=['POST'])
async def post_json(request):
    return response.json({"received": request.json})
2、args(字典):查询字符串变量

一般请求的url可以划分为:
http: 127.0.0.1:8080/user?name=zhangsan&passwd=123456#Demo
上述的url可以划分为:http协议: 服务器IP / 请求路由地址 ?get请求传递参数 # 锚点
查询字符串变量就是说URL中问号?及其后面的部分,[name=zhangsan&passwd=123456#Demo]。

3、query_args(列表)
4、files(字典):拥有name、body和type的文件对象的字典
5、form(字典):以POST方式传递的form变量
6、body(字节串):POST的原始数据。
7、headers(字典):包含请求头(headers)的不区分大小写的字典。
8、method(字符串):HTTP请求的方法,比如GET, POST等。
9、ip(字符串):客户端(浏览器)的IP地址。
10、port(字符串):客户端(浏览器)的端口地址。
11、socket(元组):客户端(浏览器)的(IP, port)
12、app:正在处理该request的Sanic应用对象的引用。
13、url:请求的完整URL。
14、cheme:请求的URL scheme:http或https。
15、host:请求的host:127.0.0.1:8800。
16、path:请求的路径path: 比如/files。
17、query_string:请求的查询字符串,name=zhangsan&passwd=123’’。
18、url_template:路由处理器匹配的模板:/post/<id>/。
19、token 授权header的值。

Sanic Request对象的属性获取:

1、get(key, default=None)

get(key, default=None) 跟dict的get方法一样。如果value是list时,只返回list的第一个元素。

2、getlist(key, default=None)

getlist(key, default=None) 返回整个list。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值