Advertisement
Oppaceted

Code2

Jan 26th, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. def get_ip():
  2.     try:
  3.         import requests
  4.     except ModuleNotFoundError:
  5.         print('Please, install requests', end=' ')
  6.         input()
  7.         exit()
  8.     return requests.get('http://ifconfig.me/ip').text
  9.  
  10. def get_info_by_ip(ip: str):
  11.     try:
  12.         import requests
  13.     except ModuleNotFoundError:
  14.         print('Please, install requests', end=' ')
  15.         input()
  16.         exit()
  17.     try:
  18.         response = requests.get(url=f'http://ip-api.com/json/{ip}').json()
  19.         if response.get('status') == 'success':
  20.             data = {
  21.                 'Country':response.get('country'),
  22.                 'Country Code':response.get('countryCode'),
  23.                 'Region':response.get('region'),
  24.                 'Region Name':response.get('regionName'),
  25.                 'City':response.get('city'),
  26.                 'ZIP':response.get('zip'),
  27.                 'lat':response.get('lat'),
  28.                 'lon':response.get('lon'),
  29.                 'Timezone':response.get('timezone'),
  30.                 'Int prov':response.get('isp'),
  31.                 'Org':response.get('org'),
  32.                 'As':response.get('as'),
  33.                 'Ip':response.get('query')
  34.             }
  35.         elif response.get('status') == 'fail':
  36.             data = {}
  37.     except requests.exceptions.ConnectionError:
  38.         print('ConnectionError')
  39.         data = {}
  40.     finally:
  41.         return data
  42. def main():
  43.     ip = get_ip()
  44.     for k,v in get_info_by_ip(ip).items():
  45.             print(f'{k}: {v}')
  46.  
  47. if __name__ == '__main__':
  48.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement