python readchar example
Example 1: python pygeoip example
def lookup(ip):
print("IP:", ip)
db = pygeoip.GeoIP("GeoLiteCity.dat")
geo_data = db.record_by_name(ip)
print("Country: " + geo_data["country_name"].encode("utf-8"))
if geo_data["city"]:
print("City: " + geo_data["city"].encode("utf-8"))
print("Longitude: " + str(geo_data["longitude"]).encode("utf-8"))
print("Latitude: " + str(geo_data["latitude"]).encode("utf-8"))
s = raw_input("\nDo you want to save the data in a .txt file? [Y/n]: ")
if s == "n" or s == "N" or s == "no" or s == "No" or s == "NO":
exit()
else:
f = open(str(ip) ,'w+')
c1 = "Country: " + geo_data["country_name"].encode("utf-8") + "\n"
f.write(str(c1))
if geo_data["city"]:
c2 = "City: " + geo_data["city"].encode("utf-8") + "\n"
f.write(str(c2))
l1 = "Longitude: " + str(geo_data["longitude"]).encode("utf-8") + "\n"
f.write(str(l1))
l2 = "Latitude: " + str(geo_data["latitude"]).encode("utf-8") +"\n"
f.write(str(l2))
f.close()
Example 2: python pygeoip example
def _country_code_from_ip(ip_addr):
"""
Return the country code associated with an IP address.
Handles both IPv4 and IPv6 addresses.
Args:
ip_addr (str): The IP address to look up.
Returns:
str: A 2-letter country code.
"""
if ip_addr.find(':') >= 0:
return pygeoip.GeoIP(settings.GEOIPV6_PATH).country_code_by_addr(ip_addr)
else:
return pygeoip.GeoIP(settings.GEOIP_PATH).country_code_by_addr(ip_addr)