From fce6f1ab1a1d8e8b09c65cd8878d06b7043fd658 Mon Sep 17 00:00:00 2001 From: felix Date: Wed, 17 Jul 2019 12:24:09 +0800 Subject: [PATCH] use form to upload file in http post request for image/vidao query --- gnes/client/http.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/gnes/client/http.py b/gnes/client/http.py index 85d5ca2c..ece5738b 100644 --- a/gnes/client/http.py +++ b/gnes/client/http.py @@ -39,13 +39,30 @@ def start(self): async def general_handler(request, parser, *args, **kwargs): try: - data = await asyncio.wait_for(request.json(), 10) + data = dict() + # # Option 1: uploading streaming chunk data + # data = b"" + # async for chunk in request.content.iter_any(): + # data += chunk + # self.logger.info("received %d content" % len(data)) + + # Option 2: uploading via Multipart-Encoded File + post_data = await request.post() + if 'query' in post_data.keys(): + _file = post_data.get('query') + self.logger.info("query request from input file: %s" % _file.filename) + data['query'] = _file.file.read() + elif 'docs' in post_data.keys(): + files = post_data.getall("docs") + self.logger.info("index request from input files: %d files" % len(files)) + data['docs'] = [_file.file.read() for _file in files] + self.logger.info('data received, beigin processing') resp = await loop.run_in_executor( executor, stub_call, - parser([d.encode() for d in data.get('docs')] if hasattr(data, 'docs') - else data.get('query').encode(), *args, **kwargs)) + parser([d for d in data.get('docs')] if hasattr(data, 'docs') + else data.get('query'), *args, **kwargs)) self.logger.info('handling finished, will send to user') return web.Response(body=json.dumps({'result': resp, 'meta': None}, ensure_ascii=False), status=200,