This repository has been archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tg_lib.py
74 lines (47 loc) · 1.69 KB
/
tg_lib.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sys
import json
import datetime
def format_timestamp(ts):
return (format_datetime(datetime.datetime.fromtimestamp(ts)))
def format_datetime(dt):
return (dt.strftime("%Y-%m-%d %H:%M:%S.%f"))
def error (string):
return("ERROR: %s" % (string))
def not_json_serializable(obj):
try:
return(obj.to_dictionary())
except AttributeError:
try:
return("scapy:%s" % (obj.command()))
except AttributeError:
return(repr(obj))
def dump_json_readable(obj):
return json.dumps(obj, indent = 4, separators=(',', ': '), sort_keys = False, default = not_json_serializable)
def dump_json_parsable(obj):
return json.dumps(obj, separators=(',', ':'), default = not_json_serializable)
def commify(string):
return str.format("{:,}", string)
def sec_to_usec(seconds):
useconds = seconds * 1e6
return(useconds)
def ip_to_int (ip):
ip_fields = ip.split(".")
if len(ip_fields) != 4:
raise ValueError("IP addresses should be in the form of W.X.Y.Z")
ip_int = 256**3 * int(ip_fields[0]) + 256**2 * int(ip_fields[1]) + 256**1 * int(ip_fields[2]) + int(ip_fields[3])
return ip_int
def int_to_ip (_int):
octet = int(_int / (256**3))
_int = _int - (octet * 256**3)
ip = str(octet)
octet = int(_int / (256**2))
_int = _int - (octet * 256**2)
ip = ip + "." + str(octet)
octet = int(_int / (256**1))
_int = _int - (octet * 256**1)
ip = ip + "." + str(octet)
_int = _int - (octet * 256**1)
ip = ip + "." + str(octet)
return ip
def calculate_latency_pps (dividend, divisor, total_rate, protocols):
return int((float(dividend) / float(divisor) * total_rate / protocols))