攻击IP来源查询

from typing import List

import time
import requests

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}

with open('ipsuccess.txt', 'r') as file:
    iplist = []
    lines = file.readlines()
    for line in lines:
        iplist.append(line.strip())

    # 创建一个新的txt文件来保存省份和城市信息
with open('province_city_info.txt', 'w',encoding='utf-8') as outfile:
    for ip in iplist:
        if not ip.startswith('192.168'):
            url = f"https://qifu-api.baidubce.com/ip/geo/v1/district?ip={ip}"
            time.sleep(0.5)
            response = requests.get(url, headers=headers)
            data = response.json()
            prov = data["data"]["prov"]
            city = data["data"]["city"]
            print(f"IP地址 {ip} 的省份为 {prov},城市为 {city}")
            # 将省份和城市信息写入到新的txt文件中,并在每个信息后面添加一个换行符
            outfile.write(f"IP地址 {ip} 的省份为 {prov},城市为 {city}\n")

你可能感兴趣的:(安全事件,python)