add HTTPPath, fix Host
[gscan_quic.git] / getip.py
blobead81fb3022d7b2b698eff0d42f1b07b2339a33d
1 #!/usr/bin/env python3
3 import json
4 import netaddr
5 import urllib.request
7 goog_url="https://www.gstatic.com/ipranges/goog.json"
8 cloud_url="https://www.gstatic.com/ipranges/cloud.json"
10 def read_url(url):
11 try:
12 s = urllib.request.urlopen(url).read()
13 return json.loads(s)
14 except urllib.error.HTTPError:
15 print("Invalid HTTP response from %s" % url)
16 return {}
17 except json.decoder.JSONDecodeError:
18 print("Could not parse HTTP response from %s" % url)
19 return {}
21 def main():
22 goog_json=read_url(goog_url)
23 cloud_json=read_url(cloud_url)
25 if goog_json and cloud_json:
26 print("{} published: {}".format(goog_url,goog_json.get('creationTime')))
27 print("{} published: {}".format(cloud_url,cloud_json.get('creationTime')))
28 goog_cidrs = netaddr.IPSet()
29 for e in goog_json['prefixes']:
30 if e.get('ipv4Prefix'):
31 goog_cidrs.add(e.get('ipv4Prefix'))
32 cloud_cidrs = netaddr.IPSet()
33 for e in cloud_json['prefixes']:
34 if e.get('ipv4Prefix'):
35 cloud_cidrs.add(e.get('ipv4Prefix'))
36 print("IP ranges for Google APIs and services default domains:")
37 for i in goog_cidrs.difference(cloud_cidrs).iter_cidrs():
38 print(i)
40 if __name__=='__main__':
41 main()