Skip to content

Instantly share code, notes, and snippets.

@sakamossan
Last active January 21, 2018 16:53
Show Gist options
  • Save sakamossan/13005be7fcad874253934a44c54021fc to your computer and use it in GitHub Desktop.
Save sakamossan/13005be7fcad874253934a44c54021fc to your computer and use it in GitHub Desktop.
#!/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)
@sakamossan
Copy link
Author

sakamossan commented Jan 21, 2018

debughttp 使い方メモ

URLを引数にとると、レスポンスボディが標準出力に出る
はじめて叩くURLの場合は実際のリクエストが飛ぶが、その際にローカルに内容を保存し
2回目以降は保存されたものを出力して実際のリクエストが飛ばないようにする

$ python -m debughttp http://example.com/ | head
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {

debughttp list

サブコマンド list でローカルにキャッシュされているファイルの一覧が見られる

$ python -m debughttp list
debug/html/example.com

そのままrmすればローカルのキャッシュファイルを全て消すことが出来る

$ python -m debughttp list | xargs rm
$ python -m debughttp list | wc -l
       0

@sakamossan
Copy link
Author

$ 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