1. 程式人生 > 其它 >利用Python編寫IP地址的物理定位工具

利用Python編寫IP地址的物理定位工具

  本工具利用網站https://app.ipgeolocation.io/提供的API對IP地址進行物理定位。

 1 import requests
 2 import sys
 3 import optparse
 4 import ipaddress
 5 
 6 
 7 class IPGeolocation:
 8     def __init__(self) -> None:
 9         self.app_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXx'  #需要在上述網站註冊,並獲得api key
10         if not self.get_params():
11 self.ip_addr = self.get_my_ip() #如果使用者沒有輸入引數,表示使用者想要查詢本機的物理位置 12 else: 13 14 self.ip_addr = self.get_params() 15 print("[-] Start to geolocate IP address: %s" % self.ip_addr) 16 self.banner() 17 18 def banner(self): 19 banner= """
20 ************************************************** 21 22 **********IP GeoLocation Tool by Jason Wong******* 23 24 ************************************************** 26 27 """ 28 print(banner) 29 30 31
def check_ip_valid(self, ip): 32 try: 33 ipaddress.ip_address(ip) #對使用者輸入的引數進行合法性判斷 34 except: 35 print("[-] Please input correct ip address") 36 sys.exit(0) 37 38 def get_params(self): 39 parser = optparse.OptionParser('Usage: <Program> -i ip address') 40 parser.add_option('-i', '--ip', dest='ip', type='string', help='Specify IP address to geolocate') 41 options, args = parser.parse_args() 42 if options.ip is None: 43 return None 44 else: 45 self.check_ip_valid(options.ip) 46 47 return options.ip 48 49 def get_my_ip(self): #如果使用者沒有輸入引數,則首先需要獲得本機的IP地址 50 """ 'https://api.ipgeolocation.io/getip' { "ip": "1.1.1.1" }""" 51 try: 52 params = {"ip": "1.1.1.1"} 53 response = requests.get(url='https://api.ipgeolocation.io/getip', params=params) 54 return response.json()['ip'] 55 except Exception as e: 56 print(e) 57 sys.exit(0) 58 59 def geo_locate(self): 60 """"curl 'https://api.ipgeolocation.io/ipgeo?apiKey=API_KEY&ip=8.8.8.8'""" 61 try: 62 url = 'https://api.ipgeolocation.io/ipgeo' 63 params = { 64 'apiKey': self.app_key, 65 'ip': self.ip_addr 66 } 67 response = requests.get(url=url, params=params).json() 68 print("\tThe IP address: %s\n" % self.ip_addr) 69 print( 70 """ 71 Information as follows: 72 Country: %s 73 Province: %s 74 City: %s 75 Latitude: %s 76 Longtitude:%s 77 ISP: %s 78 """ %(response['country_name'], response['state_prov'],response['city'], response['latitude'], response['longitude'], response['isp']) 79 ) 80 except Exception as e: 81 print(e) 82 sys.exit(0) 83 84 if __name__ == '__main__': 85 iper = IPGeolocation() 86 iper.geo_locate()

  執行效果如下所示:

# python ip_geolocation.py
[-] Start to geolocate IP address: 103.142.208.238

                **************************************************

                **********IP GeoLocation Tool by Jason Wong*******

                **************************************************

        The IP address: 103.142.208.238


                    Information as follows:
                    Country:   Cambodia
                    Province:
                    City:      Phnom Penh
                    Latitude:  11.57590
                    Longtitude:104.89940
                    ISP:       Mega Truenet Communication Co., Ltd.