python async post requests
The code in the question executes all POST requests in a series, making the code no faster than if you used requests
in a single thread. But unlike requests
, asyncio makes it possible to parallelize them in the same thread:
async def make_account():
url = "https://example.com/sign_up.php"
async with aiohttp.ClientSession() as session:
post_tasks = []
# prepare the coroutines that post
async for x in make_numbers(35691, 5000000):
post_tasks.append(do_post(session, url, x))
# now execute them all at once
await asyncio.gather(*post_tasks)
async def do_post(session, url, x):
async with session.post(url, data ={
"terms": 1,
"captcha": 1,
"email": "user%[email protected]" % str(x),
"full_name": "user%s" % str(x),
"password": "123456",
"username": "auser%s" % str(x)
}) as response:
data = await response.text()
print("-> Created account number %d" % x)
print (data)
The above code will attempt to send all the POST requests at once. Despite the intention, it will be throttled by aiohttp.ClientSession
's TCP connector which allows a maximum of 100 simultaneous connections by default. To increase or remove this limitation, you must set a custom connector on the session.