update to new geoip lib, fix the whole thing, remove the format parameter
This commit is contained in:
parent
f7be02bf0a
commit
cd763f3930
BIN
GeoLiteCity.dat
Normal file
BIN
GeoLiteCity.dat
Normal file
Binary file not shown.
40
ip.py
40
ip.py
@ -1,15 +1,12 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import cgi
|
import cgi
|
||||||
import os
|
import os
|
||||||
#import html
|
import pygeoip
|
||||||
|
|
||||||
F_PLAIN = 0
|
|
||||||
F_XML = 1
|
|
||||||
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 = {
|
||||||
@ -19,33 +16,16 @@ def main():
|
|||||||
"location": None
|
"location": None
|
||||||
}
|
}
|
||||||
header()
|
header()
|
||||||
out["ip"] = cgi.escape(os.environ["REMOTE_ADDR"])
|
client_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 == "geo":
|
||||||
r_format = params[key].value
|
gi = pygeoip.GeoIP('GeoLiteCity.dat')
|
||||||
if r_format == "xml":
|
geo_info = gi.record_by_addr(ip)
|
||||||
print("lol xml are you serious? get with the times")
|
print(geo_info)
|
||||||
elif r_format == "json":
|
else:
|
||||||
format = F_JSON
|
print(client_ip)
|
||||||
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 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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
pygeoip
|
Loading…
Reference in New Issue
Block a user