diff --git a/GeoLiteCity.dat b/GeoLiteCity.dat new file mode 100644 index 0000000..1adb8c3 Binary files /dev/null and b/GeoLiteCity.dat differ diff --git a/ip.py b/ip.py index 85b9f2b..6cd2d6a 100644 --- a/ip.py +++ b/ip.py @@ -1,15 +1,12 @@ #!/usr/bin/env python import cgi import os -#import html - -F_PLAIN = 0 -F_XML = 1 -F_JSON = 2 +import pygeoip def header(): print("Content-type: text/plain") print("") + def main(): params = cgi.FieldStorage() out = { @@ -19,33 +16,16 @@ def main(): "location": None } header() - out["ip"] = cgi.escape(os.environ["REMOTE_ADDR"]) + client_ip = cgi.escape(os.environ["REMOTE_ADDR"]) format = F_PLAIN if(len(params) > 0): for key in params: - if key == "format": - r_format = params[key].value - if r_format == "xml": - print("lol xml are you serious? get with the times") - elif r_format == "json": - format = F_JSON - elif key == "and": - extras = params[key].value.split(',') - if "geo" in extras: - from geoip import geolite2 - match = geolite2.lookup(out["ip"].encode("UTF-8")) - if match is not None: - out["country"] = match.country - out["continent"] = match.continent - out["location"] = match.location - + if key == "geo": + gi = pygeoip.GeoIP('GeoLiteCity.dat') + geo_info = gi.record_by_addr(ip) + print(geo_info) + else: + print(client_ip) - if format == F_PLAIN: - for item in out: - if out[item] is not None: - print(out[item]) - elif format == F_JSON: - import json - print(json.dumps(dict((k, v) for k, v in out.items() if v is not None))) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..11a4f3b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pygeoip