├── test.py ├── LICENSE ├── README.md └── geo.py /test.py: -------------------------------------------------------------------------------- 1 | import geo 2 | 3 | ip = geo.getIP() 4 | print(ip) 5 | 6 | country = geo.getCountry(ip, 'plain') 7 | print(country) 8 | 9 | country = geo.getCountry(ip, 'json') 10 | print(country) 11 | 12 | geoData = geo.getGeoData(ip) 13 | print(geoData) 14 | 15 | ptrData = geo.getPTR(ip) 16 | print(ptrData) 17 | 18 | geo.showIpDetails('216.239.32.0') 19 | 20 | geo.showCountryDetails('216.239.32.0') -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Vasilis G. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IP-location-tracker 2 | 3 | This is an IP location tracker made in Python 3.6, using the [urllib.request](https://docs.python.org/3/library/urllib.request.html) 4 | module and the [GeoJs](https://www.geojs.io/) API to get geolocation data. 5 | 6 | ## Usage 7 | 8 | `geo` is pretty simple to use and can provide with all the necessary details about an IP address such as: 9 | - Country (both abbreviation and full name). 10 | - Region (the area within a country in which the IP address is located). 11 | - City. 12 | - Timezone. 13 | - Latitude. 14 | - Longitude. 15 | - Organization name. 16 | 17 | Below there are some examples that demonstrate `geo`'s usage: 18 | 19 | __Getting IP address of host:__ 20 | ``` 21 | import geo 22 | 23 | ip = geo.getIP() 24 | ``` 25 | __Getting country origin of IP address:__ 26 | 27 | (The address below is one of the IP address range that Google uses.) 28 | 29 | 30 | This will return plain text by default, as we haven't set the second argument. 31 | 32 | ``` 33 | country = geo.getCountry('216.239.32.0') 34 | ``` 35 | Result: 36 | ``` 37 | US 38 | ``` 39 | This will return the country's full name. 40 | ``` 41 | country = geo.getCountry('216.239.32.0', 'plainfull') 42 | ``` 43 | Result: 44 | ``` 45 | United States 46 | ``` 47 | This will return a json response which will contains all formats of country's name and the responding IP address. 48 | ``` 49 | country = geo.getCountry('216.239.32.0', 'json') 50 | ``` 51 | Result: 52 | ``` 53 | {'country': 'US', 'country_3': 'USA', 'ip': '216.239.32.0', 'name': 'United States'} 54 | ``` 55 | 56 | __Getting geolocation data of IP address:__ 57 | ``` 58 | geoData = geo.getGeoData('216.239.32.0') 59 | ``` 60 | Returns: 61 | ``` 62 | {'organization_name': 'Google LLC', 'region': 'California', 'accuracy': 1000, 'asn': 15169, 'organization': 'AS15169 Google LLC', 'timezone': 'America/Los_Angeles', 'longitude': '-122.2971', 'country_code3': 'USA', 'area_code': '0', 'ip': '216.239.32.0', 'city': 'San Mateo', 'country': 'United States', 'continent_code': 'NA', 'country_code': 'US', 'latitude': '37.5428'} 63 | ``` 64 | 65 | __Getting a summary of all information about an IP address:__ 66 | ``` 67 | geo.showIpDetails('216.239.32.0') 68 | ``` 69 | The output will be: 70 | ``` 71 | ---------------------------------------------------------------------- 72 | HOST DETAILS 73 | ---------------------------------------------------------------------- 74 | Country: United States 75 | Organization name: Google LLC 76 | Region: California 77 | Accuracy: 1000 78 | Asn: 15169 79 | Organization: AS15169 Google LLC 80 | Timezone: America/Los_Angeles 81 | Longitude: -122.2971 82 | Country code3: USA 83 | Area code: 0 84 | Ip: 216.239.32.0 85 | City: San Mateo 86 | Country: United States 87 | Continent code: NA 88 | Country code: US 89 | Latitude: 37.5428 90 | ---------------------------------------------------------------------- 91 | ``` 92 | __Getting country details for an IP address:__ 93 | ``` 94 | geo.showCountryDetails('216.239.32.0') 95 | ``` 96 | This will return: 97 | ``` 98 | ---------------------------------------------------------------------- 99 | COUNTRY DETAILS 100 | ---------------------------------------------------------------------- 101 | Country: US 102 | Country 3: USA 103 | Ip: 216.239.32.0 104 | Name: United States 105 | ---------------------------------------------------------------------- 106 | ``` 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /geo.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import json 3 | 4 | # Header declarations for output print. 5 | hostTitle = "HOST DETAILS\n" 6 | countryTitle = "COUNTRY DETAILS\n" 7 | 8 | # Format printing constants. 9 | dotNumber = 70 10 | countryPadding = 50 11 | detailsPadding = 40 12 | 13 | ''' 14 | All available types of responses for IP along with their urls. 15 | ''' 16 | 17 | ipValidTypes = ['plain', 'json', 'jsonp'] 18 | ipPlain = 'https://get.geojs.io/v1/ip' 19 | ipJson = 'https://get.geojs.io/v1/ip.json' 20 | ipLookup = {'plain' : ipPlain, 'json' : ipJson} 21 | 22 | ''' 23 | All available types of responses for country along with their urls. 24 | ''' 25 | 26 | countryValidTypes = ['plain', 'plainfull', 'json', 'jsonp'] 27 | countryPlain = 'https://get.geojs.io/v1/ip/country' 28 | countryFullPlain = 'https://get.geojs.io/v1/ip/country/full' 29 | countryJson = 'https://get.geojs.io/v1/ip/country/{ip address}.json' 30 | countryLookup = {'plain' : countryPlain, 'plainfull' : countryFullPlain, 'json' : countryJson} 31 | 32 | ''' 33 | All available types of responses for all geo data along with their urls. 34 | ''' 35 | geoJson = 'https://get.geojs.io/v1/ip/geo/{ip address}.json' 36 | 37 | ''' 38 | All available types of responses for DNS PTR records. 39 | ''' 40 | ptrPlain = 'https://get.geojs.io/v1/dns/ptr' 41 | 42 | # Gets the response of a url that returns plain text as response. 43 | def getPlainResponse(url): 44 | return urllib.request.urlopen(url).read().decode().strip() 45 | 46 | # Gets the response of a url that returns json as response and replaces the default argument '{ip address}' with the IP address whose country we're looking 47 | def getJsonResponse(url, ipAddress): 48 | response = urllib.request.urlopen(url.replace('{ip address}',ipAddress)).read().decode() 49 | outDict = json.loads(response) 50 | return outDict 51 | 52 | # Gets host's IP address, having default 'returnType' as 'plain', which can be changed accordingly. 53 | def getIP(returnType = 'plain'): 54 | if isinstance(returnType,str): 55 | returnType = returnType.lower() 56 | if returnType in ipValidTypes: 57 | if returnType == 'plain': 58 | return getPlainResponse(ipLookup[returnType]) 59 | else: 60 | return getJsonResponse(ipLookup[returnType],'') 61 | else: 62 | raise ValueError('\'returnType\' does not belong in valid types: ' + str(ipValidTypes)) 63 | else: 64 | raise TypeError('\'returnType\' must be of type \'str\'(' + type(returnType).__name__ + ' was given).') 65 | 66 | # Gets the country of a specific IP address. 67 | def getCountry(ipAddress, returnType = 'plain'): 68 | if not isinstance(ipAddress,str): 69 | raise TypeError('\'ipAddress\' is not an instance of \'str\'('+ type(ipAddress).__name__ + ' was given).') 70 | if isinstance(returnType,str): 71 | returnType = returnType.lower() 72 | if returnType in countryValidTypes: 73 | if returnType == 'plain': 74 | return getPlainResponse(countryLookup[returnType] + '/' + ipAddress) 75 | elif returnType == 'plainfull': 76 | return getPlainResponse(countryLookup[returnType] + '/' + ipAddress) 77 | else: 78 | return getJsonResponse(countryLookup[returnType], ipAddress) 79 | else: 80 | raise ValueError('\'returnType\' does not belong in valid types: ' + str(countryValidTypes)) 81 | else: 82 | raise TypeError('\'returnType\' must be of type \'str\'(' + type(returnType).__name__ + ' was given).') 83 | 84 | # Gets all available geodata for a specific IP address. 85 | def getGeoData(ipAddress): 86 | if isinstance(ipAddress, str): 87 | return getJsonResponse(geoJson, ipAddress) 88 | else: 89 | raise TypeError("\'ipAddress\' is not an instance of list.") 90 | 91 | # Gets the DNS PTR record of an IP address, if possible. 92 | def getPTR(ipAddress): 93 | if not isinstance(ipAddress, str): 94 | raise TypeError("\'ipAddress\' is not an instance of list.") 95 | return getPlainResponse(ptrPlain) 96 | 97 | # Gets all country information for an IP address. 98 | def showCountryDetails(ip=''): 99 | result = "" 100 | if ip == '': 101 | ip = getIP('plain') 102 | countryData = getCountry(ip, 'json') 103 | result += '-' * dotNumber + '\n' 104 | result += (dotNumber//2 - len(countryTitle)//2) * ' ' + countryTitle 105 | result += '-' * dotNumber + '\n' 106 | for key, value in countryData.items(): 107 | cleanKey = key.replace('_',' ').capitalize() + ':' 108 | cleanKey = cleanKey.ljust(countryPadding, ' ') 109 | result += cleanKey + str(value) + '\n' 110 | result += '-' * dotNumber + '\n' 111 | print(result) 112 | 113 | # Get all available information provided for a specific IP address (country, location, region, etc.). 114 | def showIpDetails(ip=''): 115 | result = "" 116 | if ip == '': 117 | ip = getIP('plain') 118 | country = getCountry(ip, 'plainFull') 119 | result += '-' * dotNumber + '\n' 120 | result += (dotNumber//2 - len(hostTitle)//2) * ' ' + hostTitle 121 | result += '-' * dotNumber + '\n' 122 | result += 'Country: '.ljust(countryPadding,' ') + country + '\n' 123 | geoData = getGeoData(ip) 124 | ptrData = getPTR(ip) 125 | for key, value in geoData.items(): 126 | cleanKey = key.replace('_',' ').capitalize() + ':' 127 | cleanKey = cleanKey.ljust(countryPadding,' ') 128 | result += cleanKey + str(value) + '\n' 129 | result += '-' * dotNumber + '\n' 130 | print(result) 131 | --------------------------------------------------------------------------------