我们在来看一下上一篇文章的实例:
from sanic import Sanic
from sanic.response import json
app=Sanic()
@app.route('/')
async def test(request):
msg={
'message':'Welcome to 鳄鱼教程学习python'
}
return json(msg,ensure_ascii=False)
if __name__=='__main__':
app.run(host='127.0.0.1',port=888)
你应该注意到test函数的request参数了吧,在pycharm中是没有使用的,也就是没用的,但是必须要有,没有就会报错。request包含了客户端(浏览器)发过来的HTTP请求的各类数据,下面我们来看一下具体的属性吧!
Sanic Request对象的属性
json (JSON格式数据) – JSON body
当客户端POST来的数据是json格式时,可以通过request.json来访问:
from sanic import Sanic
from sanic import response
app=Sanic()
@app.route('/json')
async def test(request):
return response.json({"status":True,"message":request.json})
if __name__=='__main__':
app.run(host='127.0.0.1',port=888)
args (字典) – 请求参数的变量
一个请求参数就是一个类似 ?key1=value1&key2=value2 URL 的一部分。如果都被解析了,那么 args 字典数据就是 {‘key1’: [‘value1’], ‘key2’: [‘value2’]}。 这个请求的 query_string 变量保存了未被解析的字符数据。
from sanic import Sanic
from sanic.response import json
app=Sanic()
@app.route('/')
async def test(request):
return json({"status":True,"message":request.args,"url":request.url,"query":request.query_string})
if __name__=='__main__':
app.run(host='127.0.0.1',port=888)
访问http://127.0.0.1:888/?name=eyujun&age=18,就会看到请求的url参数被解析了,显示的就是:
{"status":true,"message":{"name":["eyujun"],"age":["18"]},"url":"http://127.0.0.1:888/?name=eyujun&age=18","query":"name=eyujun&age=18"}
raw_args (字典)
和query_string差不多,这个的值为字典类型,query_string的值是一个列表:
{"status":true,"message":{"name":"eyujun","age":"18"},"url":"http://127.0.0.1:888/?name=eyujun&age=18","query":"name=eyujun&age=18"}
Sanic 18.12 版本已经没有 query_args
这个属性,所以还是使用raw_args比较好点。
files (File 对象的字典)
文件列表,包括 name, body, 和 type
from sanic import Sanic
from sanic import response
app=Sanic(__name__)
@app.route('/')
async def index(request): #用于收集form表单
html = ("""<html><body>
<form action="/files" method="post" enctype="multipart/form-data">
<input type="file" name="file1" /> <br />
<input type="file" name="file2" /> <br />
<input type="submit" value="上传" />
</form>
</body></html>""")
return response.html(html)
@app.route('/files',methods=['POST'])
async def file(request): #用于展示form表单的数据信息
f_file=request.files.get('file1')
f_params={
"body":len(f_file.body),
"name":f_file.name,
"type":f_file.type,
}
return response.json({"received": True,"f_info":f_params,},ensure_ascii=False) #支持中文输出
if __name__=='__main__':
app.run(host='127.0.0.1',port=888)
form (字典) – 提交 form 变量
form肯定涉及到表单的提交,一般都是post请求,我们来修改一下代码:
from sanic import Sanic
from sanic import response
from sanic.response import json
app=Sanic(__name__)
@app.route('/')
async def index(request): #用于收集form表单
html = ("""<html><body>
<form action="/files" method="post" enctype="multipart/form-data">
<input type="file" name="file1" /> <br />
<input type="text" name="title" /> <br />
<input type="submit" value="上传" />
</form>
</body></html>""")
return response.html(html)
@app.route('/files',methods=['POST'])
async def file(request): #用于展示form表单的数据信息
return json({"received": True,"form":request.form},ensure_ascii=False) #支持中文输出
if __name__=='__main__':
app.run(host='127.0.0.1',port=888)
request对象是web应用要处理的对象,它包含了客户端(浏览器)的请求数据,通过它的各种属性来访问这些请求数据。
声明:1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!