A Python Module to wrap the IP address geolocation service from hostip.info.
Install from PyPI using
pip install pylocation
pylocation depends on the excellent requests library.
from pylocation import PyLocation # Initializing without specifying an IP Address gives us the IP information # from the machine using this module pyloc = PyLocation() # Same as above, but includes the GPS Coordinates pyloc = PyLocation(position=True) # Initializing with an IP address pyloc = PyLocation(ip='188.8.131.52') # Same as above, but includes the GPS Coordinates pyloc = PyLocation(ip='184.108.40.206', position=True) # Outputs pyloc.text # Returns the information as a unicode string pyloc.jsontext # Returns the information as a JSON string pyloc.json # Returns the information as JSON parsed to a dictionary pyloc.xmltext # Returns the information as an xml string pyloc.xml # Returns the information as an XML Element Tree """ The following returns the information as a python object """ pylocinfo = pyloc.info print pylocinfo.ip # '220.127.116.11' print pylocinfo.city # 'Mountain View, CA' print pylocinfo.country_name # 'UNITED STATES' print pylocinfo.country_code # 'US' print pylocinfo.latitude # 37.402 print pylocinfo.longitude # -122.078 """ If you're getting the info for multiple IP addresses, you don't need to create multiple PyLocation objects """ pyloc = PyLocation() pyloc.json # Returns the JSON representation of the geolocation info for the IP of the host machine pyloc.ip = '18.104.22.168' # Change the IP to 22.214.171.124 pyloc.position = True # Include the GPS Coordinates pyloc.json # Returns the JSON representation of geolocation info for IP 126.96.36.199