-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswarm.py
More file actions
47 lines (37 loc) · 1.21 KB
/
swarm.py
File metadata and controls
47 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import base64
import io
import random
import time
from uuid import uuid4
import aiohttp
from PIL import Image
DEVICE_COUNT = 1000
def dummy_bytes():
image = Image.new("RGB", (100, 100))
image_bytes = io.BytesIO()
image.save(image_bytes, format="JPEG")
return base64.b64encode(image_bytes.getvalue()).decode()
async def send_post_request(session, device_id):
url = "http://127.0.0.1:5555/buff"
data = {"deviceId": device_id, "data": dummy_bytes()}
async with session.post(url, json=data) as response:
print(
f"Device {device_id}: Status Code {response.status} Response: {await response.text()}"
)
async def device_swarm():
num_devices = random.randint(0, DEVICE_COUNT)
print(f"Device cnt {num_devices}")
async with aiohttp.ClientSession() as session:
tasks = []
for i in range(num_devices):
task = asyncio.ensure_future(
send_post_request(session, device_id=str(uuid4()))
)
tasks.append(task)
await asyncio.gather(*tasks)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
while True:
loop.run_until_complete(device_swarm())
time.sleep(5)