#example of how to use the Hostip provider
import dbus

def validKey(name_key, arr):
	for key in arr.keys():
		if key == name_key:
			return True
			break
	else:
		return False


bus = dbus.SessionBus()

try:
	geoclue = bus.get_object('org.freedesktop.Geoclue.Providers.Hostip', 
							   '/org/freedesktop/Geoclue/Providers/Hostip')
							   
	geoclue_hostipInfo = geoclue.GetAddress(dbus_interface='org.freedesktop.Geoclue.Address')
except:
	print "D-Bus error."

if validKey('country', geoclue_hostipInfo[1]):
	print "Country: " + geoclue_hostipInfo[1]['country']

if validKey('countrycode', geoclue_hostipInfo[1]):
	print "Country Code: " + geoclue_hostipInfo[1]['countrycode']

if validKey('locality', geoclue_hostipInfo[1]):
	print "City (locality): " + geoclue_hostipInfo[1]['locality']
else:
	print "The locality for this IP address is not defined. \nPlease define it at http://www.hostip.info"


