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='8.8.8.8')
# Same as above, but includes the GPS Coordinates
pyloc = PyLocation(ip='8.8.8.8', 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 # '8.8.8.8'
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 = '8.8.8.8' # Change the IP to 8.8.8.8
pyloc.position = True # Include the GPS Coordinates
pyloc.json # Returns the JSON representation of geolocation info for IP 8.8.8.8