geoip lookup added. had to downgrade to python2 :(

master
panki27 5 years ago
parent c8c3b69740
commit f7be02bf0a

30
ip.py

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python
import cgi import cgi
import os import os
import html #import html
F_PLAIN = 0 F_PLAIN = 0
F_XML = 1 F_XML = 1
@ -9,31 +9,43 @@ F_JSON = 2
def header(): def header():
print("Content-type: text/plain") print("Content-type: text/plain")
print() print("")
def main(): def main():
params = cgi.FieldStorage() params = cgi.FieldStorage()
out = { out = {
"ip": None "ip": None,
"country": None,
"continent": None,
"location": None
} }
header() header()
remote_ip = html.escape(os.environ["REMOTE_ADDR"]) out["ip"] = cgi.escape(os.environ["REMOTE_ADDR"])
format = F_PLAIN format = F_PLAIN
if(len(params) > 0): if(len(params) > 0):
for key in params: for key in params:
if key == "format": if key == "format":
r_format = params[key].value r_format = params[key].value
if r_format == "xml": if r_format == "xml":
format = F_PLAIN
print("lol xml are you serious? get with the times") print("lol xml are you serious? get with the times")
elif r_format == "json": elif r_format == "json":
format = F_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
out["ip"] = remote_ip
for item in out:
if format == F_PLAIN: if format == F_PLAIN:
for item in out:
if out[item] is not None:
print(out[item]) print(out[item])
elif format == F_JSON: elif format == F_JSON:
import json import json
print(json.dumps(out)) print(json.dumps(dict((k, v) for k, v in out.items() if v is not None)))
if __name__ == "__main__": if __name__ == "__main__":
main() main()
Loading…
Cancel
Save