added some error handling
This commit is contained in:
parent
b6958b41cf
commit
993e6feff0
40
ip-api.py
40
ip-api.py
@ -2,25 +2,37 @@ import argparse
|
|||||||
import requests
|
import requests
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
def query_api(host):
|
def query_api(host):
|
||||||
main_api = 'http://ip-api.com/json/'
|
main_api = 'http://ip-api.com/json/'
|
||||||
# For every host do an API request
|
# For every host do an API request
|
||||||
for x in host:
|
try:
|
||||||
json_data = requests.get(main_api + x).json()
|
for x in host:
|
||||||
# Print out wanted JSON data formatted nicely
|
json_data = requests.get(main_api + x).json()
|
||||||
print('\nCity\State: {}, {}\n'
|
if 'message' in json_data:
|
||||||
'Country: {}\n'
|
print('\nThe IP "{}" is {}'.format(x,
|
||||||
'ISP: {}\n'
|
json_data['message']))
|
||||||
'IP: {}\n'
|
# Print out wanted JSON data formatted nicely
|
||||||
'MX: {}'.format(
|
else:
|
||||||
json_data['city'],
|
print('\nCity\State: {}, {}\n'
|
||||||
json_data['regionName'],
|
'Country: {}\n'
|
||||||
json_data['country'],
|
'ISP: {}\n'
|
||||||
json_data['isp'],
|
'IP: {}\n'
|
||||||
json_data['query'],
|
'MX: {}'.format(
|
||||||
x))
|
json_data['city'],
|
||||||
|
json_data['regionName'],
|
||||||
|
json_data['country'],
|
||||||
|
json_data['isp'],
|
||||||
|
json_data['query'],
|
||||||
|
x))
|
||||||
|
except KeyError:
|
||||||
|
traceback.print_exc(file=sys.stdout)
|
||||||
|
print('Key Error')
|
||||||
|
print('JSON: ')
|
||||||
|
print(json_data)
|
||||||
|
|
||||||
|
|
||||||
def findMX(host):
|
def findMX(host):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user