Skip to content

Instantly share code, notes, and snippets.

@das7pad

das7pad/main.py Secret

Last active June 27, 2017 20:33
Show Gist options
  • Save das7pad/e8f63175377576a0cb3466d20a417fa5 to your computer and use it in GitHub Desktop.
Save das7pad/e8f63175377576a0cb3466d20a417fa5 to your computer and use it in GitHub Desktop.
Python aiohttp connection test
# edit0: fixed typo 'timeout_finallly' -> 'timeout_finally', update output
# edit1: added the closed status of each response to the output
# edit2: print aiohttp version
import asyncio
import aiohttp
import json
class DummyResponse(object):
closed = 'UNDEFINED'
_connection = None
@staticmethod
def close():
pass
@staticmethod
async def read():
pass
async def fetch(case):
res = DummyResponse()
try:
res = await asyncio.wait_for(aiohttp.request('GET', URLS[case]), 10)
if 'cancelled' in case:
await asyncio.sleep(10)
body = await asyncio.wait_for(res.read(),
.00000000001 if 'timeout' in case else 10)
except asyncio.TimeoutError:
error_msg = 'Request timed out'
except aiohttp.ServerDisconnectedError as err:
error_msg = 'Server disconnected error: {}'.format(err)
except aiohttp.ClientError as err:
error_msg = 'Request connection error: {}'.format(err)
finally:
if 'finally' in case:
res.close()
RESULTS[case] = ('Connection closed: %s, Response closed: %s' %
(True if res._connection is None
else res._connection.closed, res.closed))
async def run(case):
task = LOOP.create_task(fetch(case))
await asyncio.sleep(5)
task.cancel()
await task
LOOP = asyncio.get_event_loop()
CASES = ('timeout_finally', 'cancelled_finally',
'normal', 'timeout', 'cancelled')
URLS = {'normal': 'http://speedtest-fra1.digitalocean.com/10mb.test',
'timeout': 'http://speedtest-lon1.digitalocean.com/10mb.test',
'cancelled': 'http://speedtest-ams2.digitalocean.com/10mb.test',
'cancelled_finally': 'http://speedtest-ams3.digitalocean.com/10mb.test',
'timeout_finally': 'http://speedtest-nyc1.digitalocean.com/10mb.test'}
RESULTS = {}
print('aiohttp: %s' % aiohttp.__version__)
LOOP.run_until_complete(asyncio.gather(
*[LOOP.create_task(run(case))
for case in CASES],
return_exceptions=True))
print(json.dumps(RESULTS, indent=4, sort_keys=True))

aiohttp: 2.2.0

aiohttp: 2.2.0
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f3b05f9c208>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f3b05f8eb38>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f3b05f8ef98>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f3b05f8ed68>
{
    "cancelled": "Connection closed: False, Response closed: False",
    "cancelled_finally": "Connection closed: True, Response closed: True",
    "normal": "Connection closed: True, Response closed: True",
    "timeout": "Connection closed: False, Response closed: False",
    "timeout_finally": "Connection closed: True, Response closed: True"
}
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f3b05f9c438>
Unclosed connection
client_connection: Connection<('speedtest-ams2.digitalocean.com', 80, False)>

aiohttp: 2.1.0

aiohttp: 2.1.0
{
    "cancelled": "Connection closed: True, Response closed: False",
    "cancelled_finally": "Connection closed: True, Response closed: True",
    "normal": "Connection closed: True, Response closed: False",
    "timeout": "Connection closed: True, Response closed: False",
    "timeout_finally": "Connection closed: True, Response closed: True"
}
Unclosed connection
client_connection: Connection<('speedtest-ams2.digitalocean.com', 80, False)>

aiohttp: 2.0.7

aiohttp: 2.0.7
{
    "cancelled": "Connection closed: True, Response closed: False",
    "cancelled_finally": "Connection closed: True, Response closed: True",
    "normal": "Connection closed: True, Response closed: False",
    "timeout": "Connection closed: True, Response closed: False",
    "timeout_finally": "Connection closed: True, Response closed: True"
}
Unclosed connection
client_connection: Connection<('speedtest-ams2.digitalocean.com', 80, False)>

aiohttp: 2.0.0

aiohttp: 2.0.0
{
    "cancelled": "Connection closed: False, Response closed: False",
    "cancelled_finally": "Connection closed: True, Response closed: True",
    "normal": "Connection closed: True, Response closed: True",
    "timeout": "Connection closed: False, Response closed: False",
    "timeout_finally": "Connection closed: True, Response closed: True"
}
Unclosed connection
client_connection: Connection<('speedtest-ams2.digitalocean.com', 80, False)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment