-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathanalyze-ipdata-api-key
More file actions
executable file
·41 lines (32 loc) · 1.04 KB
/
analyze-ipdata-api-key
File metadata and controls
executable file
·41 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
import json
import sys
import requests
def analyze(api_key):
"""
Analyzes an ipdata.co API key.
"""
results = {
"valid": False,
"analysis": {},
}
url = f"https://api.ipdata.co?api-key={api_key}"
try:
resp = requests.get(url)
results["analysis"]["status_code"] = resp.status_code
if resp.status_code == 200:
results["valid"] = True
elif resp.status_code in (401, 403):
# 401 is 'Unauthorized', 403 can mean quota exceeded.
# In either case, the endpoint responded as expected for a bad key.
results["analysis"]["response"] = resp.json()
else:
results["analysis"]["response"] = resp.text
except requests.exceptions.RequestException as e:
results["error"] = f"request failed: {e}"
return results
if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <api-key>")
sys.exit(1)
print(json.dumps(analyze(sys.argv[1]), indent=2))