Guest User

Untitled

a guest
Dec 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import requests, json, re
  2.  
  3. verbose = False
  4.  
  5. hosts = []
  6.  
  7. urls = []
  8.  
  9. threshold = 0.04
  10.  
  11. def int_cmp(n1, n2, threshold):
  12. diff = abs(n1 - n2)
  13. thr = max(n1, n1) * threshold
  14. # print(f"n1:{n1}/n2:{n2} diff:{diff}, threshold:{thr}")
  15. return diff <= thr
  16.  
  17. def json_cmp(base, subject):
  18. if isinstance(base, list):
  19. if not isinstance(subject, list):
  20. return "base was list but subject wasn't"
  21. if(len(base) != len(subject)): return f"lengths differ: {len(base)} != {len(subject)}"
  22. base.sort(key=json.dumps)
  23. subject.sort(key=json.dumps)
  24. for i in range(0, len(base)):
  25. res = json_cmp(base[i], subject[i])
  26. if res != None: return f"list[{i}] {res}"
  27. return None
  28. elif isinstance(base, dict):
  29. if not isinstance(subject, dict):
  30. return "base was dict but subject wasn't"
  31. base_keys = base.keys()
  32. if base_keys != subject.keys():
  33. return f"keys don't match: {list(base_keys)} != {list(subject.keys())}"
  34. for k in base_keys:
  35. res = json_cmp(base[k], subject[k])
  36. if res != None: return f"dict[{k}] doesn't match: {res}"
  37. return None
  38. elif isinstance(base, int):
  39. if not int_cmp(base, subject, threshold):
  40. return f"ints {base} and {subject} differed more than {threshold}"
  41. return None
  42. elif isinstance(base, str):
  43. if base != subject: return f"simple types don't match {base} != {subject}"
  44. return None
  45.  
  46. return f"Unknown type, can't diff {type(base)}"
  47.  
  48. def diff(rs):
  49.  
  50. if len(rs) < 2:
  51. print("nothing to do")
  52. return None
  53. base = rs[hosts[0]]
  54. rs.pop(hosts[0])
  55. for host, res in rs.items():
  56. if base.status_code != res.status_code:
  57. print(f"{host} did not match status code (exp: {base.status_code}, got: {res.status_code})")
  58. continue
  59. diff = json_cmp(base.json(), res.json())
  60. if diff == None:
  61. if verbose: print(f"ok: {res.url} matched")
  62. else:
  63. print(f"ERROR: {res.url} did not match json: {diff}")
  64.  
  65. def req(host, url):
  66. return requests.get("/".join([host, re.sub("^/+", "", url)]))
  67.  
  68. if __name__ == "__main__":
  69. import sys
  70. while True:
  71. line = sys.stdin.readline()
  72. if not line: break
  73. urls.append(line.strip())
  74. for url in urls:
  75. rs = dict([ (host, req(host, url)) for host in hosts ])
  76. diff(rs)
Add Comment
Please, Sign In to add comment