def test_shortip(self): r_data = [] result = TryData() result.add(0, 0.2) result.add(1, 0.01) result.add(2, 0.1) result.addr = '1.1.1.1' for i in range(15): r_data.append((result)) t.get_graf(r_data)
def test_makesmany(self): r_data = [] result = TryData() result.add(0, 0.2) result.add(1, 0.01) result.add(2, 0.1) result.addr = '111.111.111.111' for i in range(255): r_data.append((result)) t.get_graf(r_data)
def test_makelowseconds(self): r_data = [] result = TryData() result.add(0, 0.003) result.add(1, 0.01) result.add(2, 0.000001) result.addr = '111.111.111.111' for i in range(15): r_data.append(result) t.get_graf(r_data)
def print_data(result: TryData): try: name = socket.gethostbyaddr(result.addr)[0] except socket.herror: name = 'Unknown server' finally: f_time = round(result.get_midle_sum() * 1000, 2) print(f'{result.addr:<15} {name:<50} {f_time} ms')
def get_graf(res_data): if res_data == []: result = TryData() result.ddr = 'No_Data' res_data.append(result) dpi = STANDART_DPI coef = round(len(res_data) / 25) if coef == 0: coef = 1 fig = plt.figure(dpi=dpi, figsize=(512 * coef / dpi, 480 * coef / dpi)) matplotlib.rcParams.update({'font.size': 8}) plt.title('Traceroute') xs = range(len(res_data)) plt.bar([x + 0.05 for x in xs], [v.all_try[0] for v in res_data], width=0.2, color='red', alpha=0.7, label='first try', zorder=2) plt.bar([x + 0.3 for x in xs], [v.all_try[1] for v in res_data], width=0.2, color='blue', alpha=0.7, label='second try', zorder=2) plt.bar([x + 0.55 for x in xs], [v.all_try[2] for v in res_data], width=0.2, color='green', alpha=0.7, label='third try', zorder=2) plt.xticks(xs, [d.addr for d in res_data]) fig.autofmt_xdate(rotation=90) plt.legend(loc='upper right') fig.savefig('trace.svg')
def get_route(addr: str, hop: int, base_port: int) -> list: res_data = [] complete = False for ttl in range(1, hop+1): if complete: print('Complete') break print(f'{str(ttl) + ")":<6}', end='') result = TryData() cur = None for num_of_try in range(BASE_TRY_NUM): with make_socket_icmp(ttl, base_port) as icmp_socket: icmp_socket.sendto(get_pack(), (addr, base_port + ttl)) send_time = time.time() try: select_socket = select.select([icmp_socket], [], [], MAX_TIME) if not select_socket[0]: break rec_pack, cur = icmp_socket.recvfrom(DEFAULT_COUNT_BYTE) cur = cur[0] result.addr = cur recv_time = time.time() except socket.timeout: continue else: icmp_type, *_ = struct.unpack("bbHHh", rec_pack[20:28]) if icmp_type == ICMP_TRACE or icmp_type == ICMP_NO_ROUTE: result.add(num_of_try, recv_time - send_time) elif icmp_type == ICMP_ANSWER: col = struct.calcsize("d") s_time = struct.unpack("d", rec_pack[28:28 + col])[0] result.add(num_of_try, recv_time - s_time) complete = True else: break if not result.count_of_success: print('* * *') if cur is not None: print_data(result) res_data.append(result) if cur == addr: complete = True if ttl == hop: print('End of max hop') return res_data