Search code examples
pythonanalysis

Why GeoIP showing error in log analysis?


I have this GeoIP address code and when I run this code, it shows error that there is no module named GeoIP. This is my code :

import sys
import GeoIP

gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
with open ('Desktop/trail.txt', 'r') as f:

  for line_string in f.readlines():
    line = line_string.rstrip()
    arr = line.split()

    try:
      country_code = gi.country_code_by_addr(arr[0])
      country_name = "\"" + gi.country_name_by_addr(arr[0]) + "\""

      arr.append(country_code)
      arr.append(country_name)
    except:
      arr.append("None")

    print ",".join(arr)

This is the error : line 4, in gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) GeoIP.error: [Errno 2] No such file or directory: '/usr/local/var/GeoIP/GeoIP.dat'


Solution

  • You forgot to add GeoIP.dat database which is your code using to get information
    download it from the official release Here

    • Scroll down to GeoLite Country and choose Download
    • Move the downloaded file "GeoIP.dat.gz" to /usr/local/var/GeoIP/
    • Extract it by right click and select Extract Here
      Or by the following command: gunzip GeoIP.dat.gz

    • Then will appear a file named GeoIP.dat leave it in this path

    Now you have the database file in path /usr/local/var/GeoIP/GeoIP.dat
    try to compile again and let me know if still in problem.