如何从CSV中的IP地址获取信息
我正在构成Python的模型,并在bot-iot
dataset上申请 https://research.unsw.edu.au/projects/bot-iot-dataset
我尝试了此数据集中的CSV文件中的IP地址提取信息。
功能[saddr,daddr]
通过使用Python来介绍物联网设备地址的来源和目的地(数据将模拟),我被安装了maxminddb-Geolite2
and IP2Geotools
但是结果是
在这里的妈妈,读取数据后代码,
import time
from geolite2 import geolite2
geo = geolite2.reader()
df_1 = dt.loc[:50,['saddr']]
def IP_info_1(ip):
try:
x = geo.get(ip)
except ValueError: #Faulty IP value
return np.nan
try:
return x['country']['names']['en'] if x is not None else np.nan
except KeyError: #Faulty Key value
return np.nan
s_time = time.time()
# map IP --> country
#apply(fn) applies fn. on all pd.series elements
df_1['country'] = df_1.loc[:,'saddr'].apply(IP_info_1)
print(df_1.head(), '\n')
print('Time:',str(time.time()-s_time)+'s \n')
print(type(geo.get('48.151.136.76')))
resuls如下:
saddr country
0 192.168.100.147 NaN
1 192.168.100.147 NaN
2 192.168.100.147 NaN
3 192.168.100.147 NaN
4 192.168.100.147 NaN
Time: 0.00870203971862793s
<class 'dict'>
测试另一个代码后,
import time
s_time = time.time()
from ip2geotools.databases.noncommercial import DbIpCity
df_2 = dt.loc[:50,['saddr']]
def IP_info_2(ip):
try:
return DbIpCity.get(ip, api_key = 'free').country
except:
return np.nan
df_2['country'] = df_2.loc[:, 'saddr'].apply(IP_info_2)
print(df_2.head())
print('Time:',str(time.time()-s_time)+'s')
print(type(DbIpCity.get('48.151.136.76',api_key = 'free')))
结果是:
saddr country
0 192.168.100.147 ZZ
1 192.168.100.147 ZZ
2 192.168.100.147 ZZ
3 192.168.100.147 ZZ
4 192.168.100.147 ZZ
Time: 25.913161039352417s
<class 'ip2geotools.models.IpLocation'>
此链接中的代码逐个IP地址识别国家
如何修复?
另一个问题,此功能的地址有两种格式,有什么区别?
the first format:
fe80::250:56ff:febe:254
fe80::250:56ff:febe:26db
and the scoend is like:
192.168.100.46
除了了解该位置以外,还提出了利用这些功能的任何建议吗?
I'm bulding a model by python, and apply on Bot-IoT
dataset https://research.unsw.edu.au/projects/bot-iot-dataset
I tried extract information from IP address in CSV file inside this dataset.
The features [saddr, daddr]
refers source and destination of address of IoT device (the data are simulated)
by using python, I was installed maxminddb-geolite2
and ip2geotools
but the results are ambuge
Here the code after read data,
import time
from geolite2 import geolite2
geo = geolite2.reader()
df_1 = dt.loc[:50,['saddr']]
def IP_info_1(ip):
try:
x = geo.get(ip)
except ValueError: #Faulty IP value
return np.nan
try:
return x['country']['names']['en'] if x is not None else np.nan
except KeyError: #Faulty Key value
return np.nan
s_time = time.time()
# map IP --> country
#apply(fn) applies fn. on all pd.series elements
df_1['country'] = df_1.loc[:,'saddr'].apply(IP_info_1)
print(df_1.head(), '\n')
print('Time:',str(time.time()-s_time)+'s \n')
print(type(geo.get('48.151.136.76')))
and the resuls as following:
saddr country
0 192.168.100.147 NaN
1 192.168.100.147 NaN
2 192.168.100.147 NaN
3 192.168.100.147 NaN
4 192.168.100.147 NaN
Time: 0.00870203971862793s
<class 'dict'>
after test another code
import time
s_time = time.time()
from ip2geotools.databases.noncommercial import DbIpCity
df_2 = dt.loc[:50,['saddr']]
def IP_info_2(ip):
try:
return DbIpCity.get(ip, api_key = 'free').country
except:
return np.nan
df_2['country'] = df_2.loc[:, 'saddr'].apply(IP_info_2)
print(df_2.head())
print('Time:',str(time.time()-s_time)+'s')
print(type(DbIpCity.get('48.151.136.76',api_key = 'free')))
the results are:
saddr country
0 192.168.100.147 ZZ
1 192.168.100.147 ZZ
2 192.168.100.147 ZZ
3 192.168.100.147 ZZ
4 192.168.100.147 ZZ
Time: 25.913161039352417s
<class 'ip2geotools.models.IpLocation'>
the code from this link Identifying country by IP address
How to fixed ?
Another question, there are two format in address for this features, what the difference?
the first format:
fe80::250:56ff:febe:254
fe80::250:56ff:febe:26db
and the scoend is like:
192.168.100.46
Any suggestions to take advantage of these features other than knowing the location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论