]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/webapp/backend/geoip.py
3 from databases
import Databases
4 from misc
import Singleton
7 __metaclass__
= Singleton
11 return Databases().geoip
13 def __encode_ip(self
, addr
):
14 # ip is calculated as described in http://ipinfodb.com/ip_database.php
15 a1
, a2
, a3
, a4
= addr
.split(".")
17 return int(((int(a1
) * 256 + int(a2
)) * 256 + int(a3
)) * 256 + int(a4
) + 100)
19 def get_country(self
, addr
):
20 return self
.db
.get("SELECT * FROM ip_group_country WHERE ip_start <= %s \
21 ORDER BY ip_start DESC LIMIT 1;", self
.__encode
_ip
(addr
)).country_code
.lower()
23 def get_all(self
, addr
):
24 # XXX should be done with a join
25 location
= self
.db
.get("SELECT location FROM ip_group_city WHERE ip_start <= %s \
26 ORDER BY ip_start DESC LIMIT 1;", self
.__encode
_ip
(addr
)).location
28 return self
.db
.get("SELECT * FROM locations WHERE id = %s", int(location
))
31 if __name__
== "__main__":
34 print g
.get_country("123.123.123.123")
35 print g
.get_all("123.123.123.123")