-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpstat.py
More file actions
131 lines (103 loc) · 3.68 KB
/
httpstat.py
File metadata and controls
131 lines (103 loc) · 3.68 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""Visualize curl statistics in the terminal."""
import json
import os
import subprocess
import sys
import tempfile
CURL_FORMAT = json.dumps({
"time_namelookup": "%{time_namelookup}",
"time_connect": "%{time_connect}",
"time_appconnect": "%{time_appconnect}",
"time_pretransfer": "%{time_pretransfer}",
"time_starttransfer": "%{time_starttransfer}",
"time_total": "%{time_total}",
"speed_download": "%{speed_download}",
"speed_upload": "%{speed_upload}",
"remote_ip": "%{remote_ip}",
"remote_port": "%{remote_port}",
"http_code": "%{http_code}",
"size_download": "%{size_download}",
})
SUPPORTED_METHODS = {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"}
COLORS = {
"green": "\033[32m",
"yellow": "\033[33m",
"cyan": "\033[36m",
"magenta": "\033[35m",
"red": "\033[31m",
"bold": "\033[1m",
"reset": "\033[0m",
}
def colorize(text, color):
if not sys.stdout.isatty():
return text
return f"{COLORS.get(color, '')}{text}{COLORS['reset']}"
def format_ms(seconds):
ms = float(seconds) * 1000
if ms < 1:
return "<1ms"
return f"{ms:.0f}ms"
def make_bar(label, duration_ms, max_width=40):
width = max(1, int(duration_ms / 10))
width = min(width, max_width)
bar = "█" * width
return f" {label:<20s} {bar} {duration_ms:.0f}ms"
def run(args):
url = None
curl_args = []
for arg in args:
if arg.startswith("http://") or arg.startswith("https://"):
url = arg
else:
curl_args.append(arg)
if not url:
print("Usage: httpstat <url> [curl options]")
sys.exit(1)
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as f:
output_file = f.name
try:
cmd = [
"curl", "-w", CURL_FORMAT,
"-o", output_file,
"-s", "-S",
] + curl_args + [url]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"curl error: {result.stderr.strip()}")
sys.exit(1)
data = json.loads(result.stdout)
dns = float(data["time_namelookup"]) * 1000
tcp = (float(data["time_connect"]) - float(data["time_namelookup"])) * 1000
tls = (float(data["time_appconnect"]) - float(data["time_connect"])) * 1000
server = (float(data["time_starttransfer"]) - float(data["time_appconnect"])) * 1000
transfer = (float(data["time_total"]) - float(data["time_starttransfer"])) * 1000
ip = data["remote_ip"]
port = data["remote_port"]
status = data["http_code"]
size = int(float(data["size_download"]))
print()
print(f" Connected to {colorize(f'{ip}:{port}', 'cyan')}")
print()
print(make_bar(colorize("DNS Lookup", "green"), dns))
print(make_bar(colorize("TCP Connection", "yellow"), tcp))
if tls > 0:
print(make_bar(colorize("TLS Handshake", "magenta"), tls))
print(make_bar(colorize("Server Processing", "cyan"), server))
print(make_bar(colorize("Content Transfer", "bold"), transfer))
print()
total = float(data["time_total"]) * 1000
status_color = "green" if status.startswith("2") else "yellow" if status.startswith("3") else "red"
print(f" Status: {colorize(status, status_color)} "
f"Size: {size} bytes "
f"Total: {colorize(f'{total:.0f}ms', 'bold')}")
print()
finally:
os.unlink(output_file)
def main():
if len(sys.argv) < 2:
print("Usage: httpstat <url> [curl options]")
sys.exit(1)
run(sys.argv[1:])
if __name__ == "__main__":
main()