-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetSomeIPInfo.py
More file actions
60 lines (52 loc) · 2.13 KB
/
getSomeIPInfo.py
File metadata and controls
60 lines (52 loc) · 2.13 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
import os
import sys
import json
import requests
from time import sleep
# This is just a reminder for me. I did not create this idea.
# This is an idiom from ipinfo and in example code all over the Internet.
# another approach to the multiple-prints used in main() below
# is to concatenate them using an approach like this:
# print(f"City: {jsonstr['city']}, Country: {jsonstr['country']}, Location: {jsonstr['loc']}")
def main():
#os.system('cls' if os.name == 'nt' else 'clear')
try:
ipaddress = input("ip address: ")
sleep(1)
url = "https://ipinfo.io/{}".format(ipaddress)
response = requests.get(url)
jsonstr = decode_json(response.text)
print("IP\t\t: {}".format(jsonstr['ip']))
print("Hostname\t: {}".format(jsonstr['hostname']))
print("City\t\t: {}".format(jsonstr['city']))
print("Region\t\t: {}".format(jsonstr['region']))
print("Country\t\t: {}".format(jsonstr['country']))
print("Postcode\t: {}".format(jsonstr['postal']))
print("Timezone\t: {}".format(jsonstr['timezone']))
print("Organization\t: {}".format(jsonstr['org']))
print("Location\t: {}".format(jsonstr['loc']))
except Exception as e:
print("Failed this run. Error: {} -- {}".format(e, (sys.exc_info())))
def main_iterate():
# os.system('cls' if os.name == 'nt' else 'clear')
try:
ipaddress = input("ip address: ")
sleep(1)
url = "https://ipinfo.io/{}".format(ipaddress)
response = requests.get(url)
json_str = decode_json(response.text)
for key in json_str:
print(key, ' = ', json_str[key])
except Exception as e:
print("Failed this run. Error: {} -- {}".format(e, (sys.exc_info())))
exit()
def decode_json(json_input):
return json.loads(json_input)
if __name__ == "__main__":
print("First, hard code specific key/value pairs.")
main()
# Hard-coding above works for some use cases, but other
# times just exploring the json data (below) is more appropriate
print("Now, iterate through every key/value pair.")
main_iterate()