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) |
$ curl https://gist.githubusercontent.com/sakamossan/13005be7fcad874253934a44c54021fc/raw/d704c651e84a2d5bf6b5aa7d5d46c03699a83f86/debughttp.py \
-o debughttp.py
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
debughttp
使い方メモURLを引数にとると、レスポンスボディが標準出力に出る
はじめて叩くURLの場合は実際のリクエストが飛ぶが、その際にローカルに内容を保存し
2回目以降は保存されたものを出力して実際のリクエストが飛ばないようにする
debughttp list
サブコマンド
list
でローカルにキャッシュされているファイルの一覧が見られるそのままrmすればローカルのキャッシュファイルを全て消すことが出来る