forked from mtusi2026/mtusi2026lab4
53 lines
2.4 KiB
Python
53 lines
2.4 KiB
Python
import threading
|
|
import urllib.request
|
|
import json
|
|
import uuid
|
|
import os
|
|
from PIL import Image
|
|
import io
|
|
|
|
BASE = "http://127.0.0.1:8080"
|
|
PASSWORD = "Example-only-not-a-real-secret-123"
|
|
|
|
def setup():
|
|
name = f"load_{uuid.uuid4().hex[:6]}"
|
|
req = urllib.request.Request(BASE + "/api/v1/users", data=json.dumps({"username": name, "display_name": name, "password": PASSWORD}).encode(), headers={"Content-Type": "application/json"}, method="POST")
|
|
with urllib.request.urlopen(req) as r: u = json.loads(r.read())
|
|
|
|
import base64
|
|
cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode()
|
|
req = urllib.request.Request(BASE + "/api/v1/auth/sessions", headers={"Authorization": f"Basic {cred}"}, method="POST")
|
|
with urllib.request.urlopen(req) as r: tok = json.loads(r.read())["token"]
|
|
|
|
req = urllib.request.Request(BASE + "/api/v1/chats", data=json.dumps({"title": "load", "member_ids": [u["id"]]}).encode(), headers={"Content-Type": "application/json", "Authorization": f"Bearer {tok}"}, method="POST")
|
|
with urllib.request.urlopen(req) as r: cid = json.loads(r.read())["id"]
|
|
return tok, cid
|
|
|
|
def generate_large_image():
|
|
# Генерируем картинку 3000x3000 с шумом, чтобы она весила пару мегабайт
|
|
img = Image.new('RGB', (3000, 3000))
|
|
pixels = img.load()
|
|
for x in range(0, 3000, 20):
|
|
for y in range(0, 3000, 20):
|
|
pixels[x, y] = (os.urandom(1)[0], os.urandom(1)[0], os.urandom(1)[0])
|
|
buf = io.BytesIO()
|
|
img.save(buf, format='JPEG', quality=90)
|
|
return buf.getvalue()
|
|
|
|
def upload_file(tok, cid, i, image_data):
|
|
req = urllib.request.Request(f"{BASE}/api/v1/chats/{cid}/attachments?filename=big{i}.jpg", data=image_data, headers={"Content-Type": "image/jpeg", "Authorization": f"Bearer {tok}"}, method="POST")
|
|
try:
|
|
with urllib.request.urlopen(req) as r:
|
|
print(f"Upload {i} OK")
|
|
except Exception as e:
|
|
print(f"Upload {i} failed: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
tok, cid = setup()
|
|
print("Генерация большой картинки...")
|
|
img_data = generate_large_image()
|
|
print(f"Размер картинки: {len(img_data) / 1024 / 1024:.2f} МБ")
|
|
|
|
threads = [threading.Thread(target=upload_file, args=(tok, cid, i, img_data)) for i in range(10)]
|
|
for t in threads: t.start()
|
|
for t in threads: t.join() |