From 19be32164fb4d0ef80322ebfaf7f3d4f645cb105 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sat, 8 Jul 2017 15:48:03 +0800 Subject: [PATCH] fix that aiohttp uses 'read()' on response, but tornado doesn't --- nvchecker/source/regex.py | 14 ++++++-------- nvchecker/source/tornado_httpclient.py | 4 ++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/nvchecker/source/regex.py b/nvchecker/source/regex.py index dc709e3..feb9d1b 100644 --- a/nvchecker/source/regex.py +++ b/nvchecker/source/regex.py @@ -28,12 +28,10 @@ async def get_version(name, conf): sort_version_key = sort_version_keys[conf.get("sort_version_key", "parse_version")] async with session.get(conf['url'], headers=headers, **kwargs) as res: - version = None + body = (await res.read()).decode(encoding) try: - body = (await res.read()).decode(encoding) - try: - version = max(regex.findall(body), key=sort_version_key) - except ValueError: - logger.error('%s: version string not found.', name) - finally: - return name, version + version = max(regex.findall(body), key=sort_version_key) + except ValueError: + version = None + logger.error('%s: version string not found.', name) + return name, version diff --git a/nvchecker/source/tornado_httpclient.py b/nvchecker/source/tornado_httpclient.py index e5a6737..e8f930d 100644 --- a/nvchecker/source/tornado_httpclient.py +++ b/nvchecker/source/tornado_httpclient.py @@ -59,5 +59,9 @@ class ResponseManager: async def json_response(self): return json.loads(self.body.decode('utf-8')) +async def read(self): + return self.body + HTTPResponse.json = json_response +HTTPResponse.read = read session = Session()