为什么 GeoIP 在日志分析中显示错误?

Why GeoIP showing error in log analysis?

我有这个 GeoIP 地址代码,当我 运行 这个代码时,它显示错误,没有名为 GeoIP 的模块。这是我的代码:

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)

这是错误: 第 4 行,在 gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE) GeoIP.error: [Errno 2] 没有那个文件或目录: '/usr/local/var/GeoIP/GeoIP.dat'

您忘记添加 GeoIP.dat 您的代码用来获取信息的数据库
从官方版本下载 Here

  • 向下滚动到 GeoLite 国家/地区 并选择 下载
  • 将下载的文件“GeoIP.dat.gz”移动到/usr/local/var/GeoIP/
  • 通过右键单击并select 在此处提取
    或通过以下命令将其提取:gunzip GeoIP.dat.gz

  • 然后会出现一个名为GeoIP.dat的文件留在这个路径

现在您在路径 /usr/local/var/GeoIP/GeoIP.dat
中有了数据库文件 尝试重新编译,如果仍有问题请告诉我。