GeoLite产品下载地址:http://dev.maxmind.com/zh-hans/geoip/legacy/geolite/
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import pygeoip gi = pygeoip.GeoIP(r"I:\test\GeoLiteCity.dat") def regGeoStr(ip): try: rec = gi.record_by_name(ip) city = rec['city'] country = rec['country_code3'] if city != "": geoLoc = city + ',' + country + " location: {" + str(rec['longitude']) + ", " + str(rec['latitude'])+"}" else: geoLoc = country return geoLoc except Exception as e: print(e) return "Unregistered" if __name__ == '__main__': print(regGeoStr("162.243.128.36"))