30 lines
854 B
Python
30 lines
854 B
Python
#!/usr/bin/env python3
|
|
# coding: utf-8
|
|
|
|
import geoip2.database
|
|
import sys
|
|
from collections import defaultdict
|
|
|
|
reader = geoip2.database.Reader("./dbip-country-lite-2025-02.mmdb")
|
|
|
|
try:
|
|
with sys.stdin as file:
|
|
for rec in file:
|
|
try:
|
|
parts = rec.strip().split(" ")
|
|
ip = parts[0]
|
|
from_country = None
|
|
from_continent = None
|
|
try:
|
|
from_continent = reader.country(ip).continent.code
|
|
from_country = reader.country(ip).country.name
|
|
except geoip2.errors.AddressNotFoundError:
|
|
from_country = "Unknown"
|
|
print("{}|{}|{}".format(from_continent, from_country, "|".join(parts)))
|
|
except:
|
|
pass
|
|
except KeyboardInterrupt:
|
|
exit(0)
|
|
except:
|
|
raise
|