import sys, os, time, subprocess, urllib.parse OUT = os.path.dirname(os.path.abspath(__file__)) UA = "Mozilla/5.0" def call(name, base, params, wait=8): path = os.path.join(OUT, name + ".xml") if os.path.exists(path) and os.path.getsize(path) > 500: print("SKIP(cached)", name); return path url = "http://www.law.go.kr/DRF/" + base args = ["curl","-sS","--http1.1","-A",UA,"-G","--max-time","60"] for k,v in params.items(): args += ["--data-urlencode", f"{k}={v}"] args += [url] for attempt,delay in enumerate([0,12,24]): if delay: time.sleep(delay) r = subprocess.run(args, capture_output=True) body = r.stdout if r.returncode==0 and len(body)>500: open(path,"wb").write(body) print("OK", name, len(body)); time.sleep(wait); return path print("RETRY", name, r.returncode, len(body), r.stderr[:200]) open(path+".fail","wb").write(body) print("FAIL", name); time.sleep(wait); return None