forked from atheurer/trafficgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtg_lib.py
56 lines (34 loc) · 1.31 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
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 calculate_latency_pps (dividend, divisor, total_rate, protocols):
return int((float(dividend) / float(divisor) * total_rate / protocols))