improve a python, socket io code
Budget: $10 – $100 USD
Python AIOHTTP multiple tcpconnector
-----------------------------------------
We want to use the ip address in the tcp connector as multi. that is, we want to define more than one ip address and send requests with these ip addresses. to be used with the below code structure. It works with only 1 ip address of the tcp connector. Need to do it with multiple and random ip addresses. Fast is our preference. and the reason we prefer aiohttp is speed. Running code needs to run fast. After the tcp connector connection, please increase the for loop to 100 or 1000 and check the speed or implment multithreading for fast running.
-------
CODE
-------
import aiohttp
import asyncio
import time
import socket
start_time = time.time()
async def main():
connector = aiohttp.TCPConnector(local_addr=('2a05:4e0::1', 0), family=socket.AF_INET6)
async with aiohttp.ClientSession(connector=connector) as session:
tasks = []
for x in range(5):
task = asyncio.ensure_future(fetch(session, x))
tasks.append(task)
response = await asyncio.gather(*tasks)
print(response)
async def fetch(session, x):
url = f'http://ipv6.google.com/'
async with session.get(url) as response:
result_data = await response.text()
return x
asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))
-----------------------------------------
We want to use the ip address in the tcp connector as multi. that is, we want to define more than one ip address and send requests with these ip addresses. to be used with the below code structure. It works with only 1 ip address of the tcp connector. Need to do it with multiple and random ip addresses. Fast is our preference. and the reason we prefer aiohttp is speed. Running code needs to run fast. After the tcp connector connection, please increase the for loop to 100 or 1000 and check the speed or implment multithreading for fast running.
-------
CODE
-------
import aiohttp
import asyncio
import time
import socket
start_time = time.time()
async def main():
connector = aiohttp.TCPConnector(local_addr=('2a05:4e0::1', 0), family=socket.AF_INET6)
async with aiohttp.ClientSession(connector=connector) as session:
tasks = []
for x in range(5):
task = asyncio.ensure_future(fetch(session, x))
tasks.append(task)
response = await asyncio.gather(*tasks)
print(response)
async def fetch(session, x):
url = f'http://ipv6.google.com/'
async with session.get(url) as response:
result_data = await response.text()
return x
asyncio.run(main())
print("--- %s seconds ---" % (time.time() - start_time))