flags2_asyncio with closing rehttp response
This commit is contained in:
parent
333615d051
commit
56094ef0f0
@ -18,7 +18,7 @@ Sample run::
|
|||||||
# BEGIN FLAGS2_ASYNCIO_TOP
|
# BEGIN FLAGS2_ASYNCIO_TOP
|
||||||
import asyncio
|
import asyncio
|
||||||
import collections
|
import collections
|
||||||
from contextlib import closing
|
import contextlib
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
@ -41,7 +41,7 @@ class FetchError(Exception): # <1>
|
|||||||
def get_flag(base_url, cc): # <2>
|
def get_flag(base_url, cc): # <2>
|
||||||
url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
|
url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
|
||||||
resp = yield from aiohttp.request('GET', url)
|
resp = yield from aiohttp.request('GET', url)
|
||||||
with closing(resp):
|
with contextlib.closing(resp):
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
image = yield from resp.read()
|
image = yield from resp.read()
|
||||||
return image
|
return image
|
||||||
|
@ -81,16 +81,14 @@ def downloader_coro(cc_list, base_url, verbose, concur_req):
|
|||||||
error_msg = exc.__cause__.args[0]
|
error_msg = exc.__cause__.args[0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
error_msg = exc.__cause__.__class__.__name__
|
error_msg = exc.__cause__.__class__.__name__
|
||||||
|
if verbose and error_msg:
|
||||||
|
msg = '*** Error for {}: {}'
|
||||||
|
print(msg.format(country_code, error_msg))
|
||||||
|
status = HTTPStatus.error
|
||||||
else:
|
else:
|
||||||
error_msg = ''
|
|
||||||
status = res.status
|
status = res.status
|
||||||
|
|
||||||
if error_msg:
|
|
||||||
status = HTTPStatus.error
|
|
||||||
counter[status] += 1
|
counter[status] += 1
|
||||||
if verbose and error_msg:
|
|
||||||
msg = '*** Error for {}: {}'
|
|
||||||
print(msg.format(country_code, error_msg))
|
|
||||||
|
|
||||||
return counter
|
return counter
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user