forked from vain/asciiworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciiworld-ip-geo
executable file
·37 lines (30 loc) · 963 Bytes
/
asciiworld-ip-geo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
from pygeoip import GeoIP
import http.client
import sys
# Path to the database, Arch Linux, package geoip-citydata. Replace this
# with an appropriate path on your system.
geoip_city_database = "/usr/share/GeoIP/GeoIPCity.dat"
geoip_city_database_v6 = "/usr/share/GeoIP/GeoIPCityv6.dat"
addr = None
if len(sys.argv) > 1:
addr = sys.argv[1]
else:
try:
c = http.client.HTTPConnection('ifconfig.co')
c.request('GET', '/', headers={'User-Agent': 'curl/7.40.0'})
r = c.getresponse()
if r.status == 200:
addr = r.read().decode('ascii').strip()
except:
pass
if addr is None:
print('No IP address given and auto-discovery failed.', file=sys.stderr)
sys.exit(1)
if ":" in addr:
gi = GeoIP(geoip_city_database_v6)
else:
gi = GeoIP(geoip_city_database)
gir = gi.record_by_addr(addr)
if gir is not None:
print('{} {}'.format(gir['latitude'], gir['longitude']))