ip2location python库中的错误

时间:2017-06-17 14:42:10

标签: python

我正在使用ip2location Python库找出相应ip地址的位置。我正在尝试打开一个包含ip地址列表的文件,并通过它找出相应的位置。

import IP2Location;

IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-.       LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN");//This is sample database
File1=open('test_ip.txt','r');//This is file containing ipaddress
Line=File1.readline();
While line:
    rec = IP2LocObj.get_all(Line);
    Line=File1.readline();
    print rec.country_short

此代码出错。您可以在此处查看示例代码http://www.ip2location.com/developers/python

1 个答案:

答案 0 :(得分:0)

请使用以下Python代码。

import IP2Location;

IP2LocObj = IP2Location.IP2Location();
IP2LocObj.open("IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE-SAMPLE.BIN"); # This is sample database
with open('test_ip.txt') as f: # file containing ip addresses
    for line_terminated in f:
        line = line_terminated.rstrip('\r\n'); # strip newline
        if line: # non-blank lines
            print line
            rec = IP2LocObj.get_all(line);
            print rec.country_short