Python:将 IP 地址列表作为字符串列表传递
Python: Passing a list of IP addresses as a list of strings
我的代码旨在从文本文件中对 IP 地址进行地理定位。我在最后一节遇到了麻烦。当我 运行 代码时,我收到来自 map_ip.update 行的投诉:socket.error: illegal IP address string passed to inet_pton
当我使用 print
语句进行故障排除时,我得到以下格式:
['$ ip address']
['$ ip address']
['$ ip address']
如何让 country_name_by_addr()
以正确的格式读取每个 IP 地址?我的 IP 地址似乎被格式化为单个列表中的字符串列表。
# script that geo-locates IP addresses from a consolidated dictionary
import pygeoip
import itertools
import re
# initialize dictionary for IP addresses
count = {}
"""
This loop reads text file line-by-line and
returns one-to-one key:value pairs of IP addresses.
"""
with open('$short_logins.txt path') as f:
for cnt, line in enumerate(f):
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
count.update({cnt: ip})
cnt += 1
"""
This line consolidates unique IP addresses. Keys represent how
many times each unique IP address occurs in the text file.
"""
con_count = [(k, len(list(v))) for k, v in itertools.groupby(sorted(count.values)))]
"""
Country lookup:
This section passes each unique IP address from con_count
through country name database. These IP address are not required
to come from con_count.
"""
map_ip = {}
gi = pygeoip.GeoIP('$GeoIP.dat path')
for i in count.itervalues():
map_ip.update({i: gi.country_name_by_addr(i)})
print map_ip
所以我昨天通过取消正则表达式解决了这个难题:
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
我找到了一个更简单的解决方案,方法是去除文件中的空格并检查是否考虑了 IP 地址。 IP 地址都在第三列,因此 [2]:
ip = line.split()[2]
if ip in count:
count[ip] += 1
else:
count.update({ip: 1})
我也删除了 con_count 行。 Pygeoip 函数更容易接受不是由正则表达式组成的列表。
我的代码旨在从文本文件中对 IP 地址进行地理定位。我在最后一节遇到了麻烦。当我 运行 代码时,我收到来自 map_ip.update 行的投诉:socket.error: illegal IP address string passed to inet_pton
当我使用 print
语句进行故障排除时,我得到以下格式:
['$ ip address']
['$ ip address']
['$ ip address']
如何让 country_name_by_addr()
以正确的格式读取每个 IP 地址?我的 IP 地址似乎被格式化为单个列表中的字符串列表。
# script that geo-locates IP addresses from a consolidated dictionary
import pygeoip
import itertools
import re
# initialize dictionary for IP addresses
count = {}
"""
This loop reads text file line-by-line and
returns one-to-one key:value pairs of IP addresses.
"""
with open('$short_logins.txt path') as f:
for cnt, line in enumerate(f):
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
count.update({cnt: ip})
cnt += 1
"""
This line consolidates unique IP addresses. Keys represent how
many times each unique IP address occurs in the text file.
"""
con_count = [(k, len(list(v))) for k, v in itertools.groupby(sorted(count.values)))]
"""
Country lookup:
This section passes each unique IP address from con_count
through country name database. These IP address are not required
to come from con_count.
"""
map_ip = {}
gi = pygeoip.GeoIP('$GeoIP.dat path')
for i in count.itervalues():
map_ip.update({i: gi.country_name_by_addr(i)})
print map_ip
所以我昨天通过取消正则表达式解决了这个难题:
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
我找到了一个更简单的解决方案,方法是去除文件中的空格并检查是否考虑了 IP 地址。 IP 地址都在第三列,因此 [2]:
ip = line.split()[2]
if ip in count:
count[ip] += 1
else:
count.update({ip: 1})
我也删除了 con_count 行。 Pygeoip 函数更容易接受不是由正则表达式组成的列表。