Last active
January 21, 2018 16:53
-
-
Save sakamossan/13005be7fcad874253934a44c54021fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import os | |
import re | |
import urllib.request | |
import urllib.parse | |
ROOT_DIR = os.path.abspath(__file__ + '/../') | |
def _http_request(url: str, **kw) -> bytearray: | |
decode = kw.pop('decode', 'utf8') | |
def req(): | |
req = urllib.request.Request(url, **kw) # closure | |
res = urllib.request.urlopen(req) | |
if (res.status / 100) != 2: | |
raise IOError("http failed. status:{} url:{}".format( | |
res.status, url)) | |
return res.read() | |
filename = '{}/debug/html/{}'.format( | |
ROOT_DIR, re.sub(r'https?://', '', url.rstrip('/'))) | |
if not os.path.exists(filename): | |
dirname = os.path.dirname(filename) | |
if not os.path.exists(dirname): | |
os.makedirs(dirname) | |
resbody = req() | |
with open(filename, 'w') as f: | |
f.write(resbody.decode(decode)) | |
return open(filename, 'r').read() | |
if __name__ == '__main__': | |
if len(sys.argv) is 2: | |
if sys.argv[1] == "list": | |
for root, dirs, files in os.walk(ROOT_DIR + "/debug/html"): | |
for filename in files: | |
fullpath = os.path.join(root, filename) | |
print(fullpath.lstrip(ROOT_DIR)) | |
else: | |
url = sys.argv[1] | |
print(_http_request(url)) | |
else: | |
msg = "url required. eg: $ python -m debughttp http://example.com" | |
sys.stderr.write(msg) | |
exit(1) |
Author
sakamossan
commented
Jan 21, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment