阅读背景:

Python开发【模块】:aiohttp(二)

来源:互联网 

AIOHTTP

 

1、文件上传

① 单个文件上传

服务端

 async def post(self, request):
        reader = await request.multipart()
        # /!\ 不要忘了这步。(至于为什么请搜索 Python 生成器/异步)/!\
        file = await reader.next()
        filename = file.filename
        # 如果是分块传输的,别用Content-Length做判断。
        size = 0
        with open(filename, 'wb') as f:
            while True:
                chunk = await file.read_chunk()  # 默认是8192个字节。
                if not chunk:
                    break
                size += len(chunk)
                f.write(chunk)

        return web.Response(text='{} sized of {} successfully stored'
                                 ''.format(filename, size))
 as



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: