-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·36 lines (33 loc) · 1.12 KB
/
main.py
File metadata and controls
executable file
·36 lines (33 loc) · 1.12 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
#!/usr/bin/env python3
"""
Example tool: simple HTTP header utils & demo functions
This is a non-destructive example to make repo look real.
"""
import argparse, sys, json
from urllib.request import Request, urlopen
def fetch_headers(url, timeout=6):
headers = {"User-Agent": "DarkGithub-Example/0.3"}
req = Request(url, headers=headers, method="GET")
with urlopen(req, timeout=timeout) as r:
return dict(r.getheaders()), r.getcode(), r.geturl()
def cli():
p = argparse.ArgumentParser(prog="main.py")
p.add_argument("url", help="URL to fetch headers for")
p.add_argument("--json", action="store_true", help="output JSON")
args = p.parse_args()
try:
hdrs, code, final = fetch_headers(args.url)
except Exception as e:
print("error:", e, file=sys.stderr)
return 2
out = {"url": final, "status": code, "headers": hdrs}
if args.json:
print(json.dumps(out, indent=2))
else:
print("URL:", final)
print("Status:", code)
for k,v in hdrs.items():
print(f"{k}: {v}")
return 0
if __name__ == "__main__":
sys.exit(cli())