Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1. from typing import Union
  2.  
  3. import time
  4. import aiohttp
  5. import asyncio
  6. import async_timeout
  7.  
  8.  
  9. async def send_post(url: str, post_data: Union[str, bytes, dict], timeout=60) -> bool:
  10.     with async_timeout.timeout(timeout):
  11.         async with aiohttp.ClientSession() as session:
  12.             async with session.post(url, data=post_data) as response:
  13.                 if response.status == 200:
  14.                     return True
  15.     return False
  16.  
  17.  
  18. list_requests = [
  19.     # URL, POSTDATA
  20.     ('http://httpbin.org/post', {'a': 1, 'b': 2}),
  21.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  22.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  23.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  24.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  25.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  26.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  27.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  28.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  29.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  30.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  31.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  32.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  33.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  34.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  35.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  36.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  37.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  38.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  39.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  40.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  41.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  42.     ('http://httpbin.org/post', {'c': 1, 'd': 2}),
  43. ]
  44.  
  45.  
  46. list_tasks = list()
  47. for args in list_requests:
  48.     list_tasks.append(send_post(*args))
  49.  
  50.  
  51. start_time = time.time()
  52. loop = asyncio.get_event_loop()
  53. loop.run_until_complete(asyncio.wait(list_tasks))
  54.  
  55. print('end time: {}'.format(time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement