有人能帮我理解不同方法之间的区别吗:
request.bounded_stream.read()
request.stream.read()
request.get_media()
它们似乎做了同样的事情,但使用stream或bounded_stream提供了一个类似字节的对象
class test_dev(object):
async def on_post(self, request, response):
obj = await request.bounded_stream.read()
print(obj)
class test_dev(object):
async def on_post(self, request, response):
obj = await request.stream.read()
print(obj)
class test_dev(object):
async def on_post(self, request, response):
obj = await request.get_media()
print(obj)
stream
和bounded_stream
是类似文件的包装器,用于访问来自服务器的请求正文数据流。这些是不可入座的,据我所知,也不可能偷看它们它们之间的区别在于后者绑定到请求的
content_length
,其中stream
的行为可能因系统而异。这在official documentation中有详细说明至于
get_media()
,它是media
属性的包装器。从documentation中,您可以阅读:所以,当您第一次访问
request.media
时,请求流被消耗和缓存,这意味着从那一刻起stream
和bounded_stream
将返回并清空数据流。默认情况下,应用程序将假定内容类型为application/json
,并使用json
库对内容进行序列化和反序列化文档中不太清楚的是,如果您选择访问并因此使用
stream
或bounded_stream
,那么访问media
将返回一个错误。 如果您选择自己访问stream
或bounded_stream
,您还必须自己存储数据相关问题 更多 >
编程相关推荐