Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests, json, re
- verbose = False
- hosts = []
- urls = []
- threshold = 0.04
- def int_cmp(n1, n2, threshold):
- diff = abs(n1 - n2)
- thr = max(n1, n1) * threshold
- # print(f"n1:{n1}/n2:{n2} diff:{diff}, threshold:{thr}")
- return diff <= thr
- def json_cmp(base, subject):
- if isinstance(base, list):
- if not isinstance(subject, list):
- return "base was list but subject wasn't"
- if(len(base) != len(subject)): return f"lengths differ: {len(base)} != {len(subject)}"
- base.sort(key=json.dumps)
- subject.sort(key=json.dumps)
- for i in range(0, len(base)):
- res = json_cmp(base[i], subject[i])
- if res != None: return f"list[{i}] {res}"
- return None
- elif isinstance(base, dict):
- if not isinstance(subject, dict):
- return "base was dict but subject wasn't"
- base_keys = base.keys()
- if base_keys != subject.keys():
- return f"keys don't match: {list(base_keys)} != {list(subject.keys())}"
- for k in base_keys:
- res = json_cmp(base[k], subject[k])
- if res != None: return f"dict[{k}] doesn't match: {res}"
- return None
- elif isinstance(base, int):
- if not int_cmp(base, subject, threshold):
- return f"ints {base} and {subject} differed more than {threshold}"
- return None
- elif isinstance(base, str):
- if base != subject: return f"simple types don't match {base} != {subject}"
- return None
- return f"Unknown type, can't diff {type(base)}"
- def diff(rs):
- if len(rs) < 2:
- print("nothing to do")
- return None
- base = rs[hosts[0]]
- rs.pop(hosts[0])
- for host, res in rs.items():
- if base.status_code != res.status_code:
- print(f"{host} did not match status code (exp: {base.status_code}, got: {res.status_code})")
- continue
- diff = json_cmp(base.json(), res.json())
- if diff == None:
- if verbose: print(f"ok: {res.url} matched")
- else:
- print(f"ERROR: {res.url} did not match json: {diff}")
- def req(host, url):
- return requests.get("/".join([host, re.sub("^/+", "", url)]))
- if __name__ == "__main__":
- import sys
- while True:
- line = sys.stdin.readline()
- if not line: break
- urls.append(line.strip())
- for url in urls:
- rs = dict([ (host, req(host, url)) for host in hosts ])
- diff(rs)
Add Comment
Please, Sign In to add comment