我已经下载了csv格式的IP2Proxy™ LITE数据库,下面是其中的数据
任何人都可以帮助我如何使用 python 或 powershell 读取 IP 地址,因为它看起来是纯数字。另请解释一下第一列代表什么。
以下是将这些值转换为 IPv4 地址的字符串表示形式的方法:
def convert(ip):
def _genip(n):
for shift in range(24, -8, -8):
yield f'{(n >> shift) & 0xff:03d}'
return '.'.join(list(_genip(int(ip))))
print(convert('34676992'))
输出:
002.017.033.000
问:
还请解释一下第一列代表什么。
答:
IP 号码范围从 0 到 4294967295。相当于 IP 地址 格式为0.0.0.0到255.255.255.255,涵盖了所有IP地址 在 IPv4 中。 来源
问:
但是无法将值“2147483648”转换为类型“System.Int32”。 错误:“对于 Int32 来说,值太大或太小。”
答:
UInt32 的最大值为 0xFFFFFFFF(或十进制的 4294967295)。 来源
问:
有人可以帮我如何使用 python 读取 ip 地址吗
答: 我提供了 example 作为 LLM 的输入,以生成
convert_to_ipv4
,然后我尝试使用 tablib 数据集来转换列,将我的错误交给 LLM 以生成 load_ipdb()
,然后继续从pymetasploit3 README.md(不包括)。
#! /usr/bin/env python
"""
ip2location to pandas dataframe
[source](https://blog.ip2location.com/knowledge-base/convert-ip2location-csv-data-into-ip-ranges-or-cidr/)
"""
def convert_to_ipv4(ip_string:str)->str:
ip_number = int(ip_string)
binary_ip = format(ip_number, '032b') # Convert the IP number to binary and pad with zeroes
segments = [binary_ip[i:i+8] for i in range(0, 32, 8)] # Split the binary number into segments of 8 digits
decimal_segments = [int(segment, 2) for segment in segments] # Convert each segment back to decimal
ipv4_address = '.'.join(map(str, decimal_segments)) # Join the decimal segments using a dot to form the IPv4 address
#print(f'{ip_number} --> {ipv4_address}')
return ipv4_address
import pandas as pd
def load_ipdb(csv: str = "IP2LOCATION-LITE-DB1.CSV") -> pd.DataFrame:
data = pd.read_csv(csv, header=None, names=['from', 'to', 'cc', 'name']) # Load the CSV file into a DataFrame
data['from'] = data['from'].apply(convert_to_ipv4) # Convert the 'from' and 'to' columns to standard IPv4 addresses
data['to'] = data['to'].apply(convert_to_ipv4)
return data
from typing import Iterator
from itertools import starmap
def extract_range(index:int, row)->str:
return '%s-%s' % (row['from'], row['to'],)
def extract_ranges(data:pd.DataFrame, name:str)->str:
#data.set_index(['name']) # TODO since this is used in a read-loop,
#matches = data.loc[name] # an index should have better performance
matches = data.loc[data['name'] == name]
ranges = starmap(extract_range, matches.iterrows())
return ','.join(ranges)
if __name__ == '__main__':
data :pd.DataFrame = load_ipdb()
name :str = input('country name: ')
if (name.lower() in ['quit', 'exit', 'done',]):
print('exiting', file=sys.stderr)
break # read loop redacted
ranges:str = extract_ranges(data, name)
# msfrpcd/dbnmap example redacted
__author__ : str = "you.com"
__maintainer__: str = "@lmaddox"