def multipart(body, **header_params): """Converts multipart form data into native Python objects""" if header_params and 'boundary' in header_params: if type(header_params['boundary']) is str: header_params['boundary'] = header_params['boundary'].encode() parser = MultipartParser(stream=body, boundary=header_params['boundary'], disk_limit=17179869184) form = dict(zip([p.name for p in parser.parts()],\ [(p.filename,p.file) if p.filename else p.file.read().decode() for p in parser.parts()])) return form
async def handle(self, body): ctype = dict(self.scope['headers'])[b'content-type'] _, options = parse_options_header(ctype.decode()) mp = MultipartParser(BytesIO(body), options['boundary']) content = '' for part in mp.parts(): if part.filename: content = part.value await self.send_response(200, b'%d' % len(content), headers=[ ('Content-Type', 'text/plain; charset=utf-8'), ])