-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinger.py
51 lines (34 loc) · 1.04 KB
/
pinger.py
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
48
49
50
51
from time import time
import asyncio
import struct
host, port = input('Host: ').split(':')
port = int(port)
cons = 100
response_times = []
handshake_payload = b'\x10\x00\x04\nxenonmc.mlJ\xbd\x01'
request_payload = b'\x01\x00'
pingpong_payload = b"\t\x01.K\xdb\xf7\xc9\x84';"
async def ping():
try:
r, w = await asyncio.open_connection(host, port)
w.write(handshake_payload)
await w.drain()
w.write(request_payload)
await w.drain()
start = time()
w.write(pingpong_payload)
await w.drain()
await r.read(8)
return time() - start
except ConnectionError:
return -1
async def main():
tasks = []
for _ in range(cons):
tasks.append(asyncio.create_task(ping()))
await asyncio.wait(tasks)
times = [t for t in [t.result() for t in tasks] if t != -1]
avg = round((sum(times) / len(times)) * 1000, 2)
max_ = round(max(times) * 1000, 2)
print(f'Average: {avg}ms | Max: {max_}ms | Dropped: {len(tasks)-len(times)}')
asyncio.run(main())