在上一篇的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。

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐