|
| 1 | +import requests |
| 2 | +import json |
| 3 | + |
| 4 | +def query_ip_info(ip_address): |
| 5 | + """ |
| 6 | + 高精度IP查询工具箱 |
| 7 | + 查询指定IP地址的详细信息 |
| 8 | + |
| 9 | + 参数: |
| 10 | + ip_address (str): 要查询的IP地址 |
| 11 | + |
| 12 | + 返回: |
| 13 | + dict: 包含IP详细信息的字典 |
| 14 | + """ |
| 15 | + url = "https://api.ip77.net/ip2/v4/" |
| 16 | + |
| 17 | + payload = {'ip': ip_address} |
| 18 | + |
| 19 | + headers = { |
| 20 | + 'User-Agent': "Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Mobile/15E148 Safari/604.1", |
| 21 | + 'Accept': "application/json, text/javascript, */*; q=0.01", |
| 22 | + 'sec-fetch-site': "cross-site", |
| 23 | + 'accept-language': "zh-CN,zh-Hans;q=0.9", |
| 24 | + 'sec-fetch-mode': "cors", |
| 25 | + 'origin': "https://uutool.cn", |
| 26 | + 'referer': "https://uutool.cn/", |
| 27 | + 'sec-fetch-dest': "empty" |
| 28 | + } |
| 29 | + |
| 30 | + try: |
| 31 | + response = requests.post(url, data=payload, headers=headers, timeout=10) |
| 32 | + response.raise_for_status() |
| 33 | + |
| 34 | + data = response.json() |
| 35 | + |
| 36 | + if data.get('code') == 0: |
| 37 | + ip_data = data['data'] |
| 38 | + result = { |
| 39 | + '归属地': f"{ip_data.get('country', '')}{ip_data.get('province', '')}{ip_data.get('city', '')}{ip_data.get('district', '')}{ip_data.get('street', '')}", |
| 40 | + '运营商': ip_data.get('isp', ''), |
| 41 | + '基本信息': { |
| 42 | + 'IP地址': ip_data.get('ip', ''), |
| 43 | + '数字地址': ip_data.get('ip_int', ''), |
| 44 | + '洲': ip_data.get('continent', ''), |
| 45 | + '国家': ip_data.get('country', ''), |
| 46 | + '省份': ip_data.get('province', ''), |
| 47 | + '城市': ip_data.get('city', ''), |
| 48 | + '区县': ip_data.get('district', ''), |
| 49 | + '街道': ip_data.get('street', ''), |
| 50 | + '区划编码': ip_data.get('area_code', ''), |
| 51 | + '邮政编码': ip_data.get('zip_code', ''), |
| 52 | + '经度': ip_data.get('longitude', ''), |
| 53 | + '纬度': ip_data.get('latitude', ''), |
| 54 | + '时区': ip_data.get('time_zone', '') |
| 55 | + }, |
| 56 | + '街道定位历史': [f"历史{i+1} {address}" for i, address in enumerate(ip_data.get('street_history', []))], |
| 57 | + 'IP风险信息': { |
| 58 | + 'IP地址': ip_data.get('ip', ''), |
| 59 | + '风险分数': ip_data.get('risk', {}).get('risk_score', ''), |
| 60 | + '风险等级': ip_data.get('risk', {}).get('risk_level', ''), |
| 61 | + '是否为代理': ip_data.get('risk', {}).get('is_proxy', ''), |
| 62 | + '代理类型': ip_data.get('risk', {}).get('proxy_type', ''), |
| 63 | + '风险标签': ip_data.get('risk', {}).get('risk_tag', '') |
| 64 | + } |
| 65 | + } |
| 66 | + return result |
| 67 | + else: |
| 68 | + return {'error': f'API返回错误: {data.get("message", "未知错误")}'} |
| 69 | + |
| 70 | + except requests.exceptions.RequestException as e: |
| 71 | + return {'error': f'网络请求失败: {str(e)}'} |
| 72 | + except json.JSONDecodeError as e: |
| 73 | + return {'error': f'JSON解析失败: {str(e)}'} |
| 74 | + except Exception as e: |
| 75 | + return {'error': f'未知错误: {str(e)}'} |
| 76 | + |
| 77 | +def format_ip_info(ip_info): |
| 78 | + """ |
| 79 | + 格式化IP查询结果 |
| 80 | + |
| 81 | + 参数: |
| 82 | + ip_info (dict): IP查询结果字典 |
| 83 | + |
| 84 | + 返回: |
| 85 | + str: 格式化的查询结果 |
| 86 | + """ |
| 87 | + if 'error' in ip_info: |
| 88 | + return f"查询失败: {ip_info['error']}" |
| 89 | + |
| 90 | + output = [] |
| 91 | + output.append("=" * 50) |
| 92 | + output.append("高精度IP查询结果") |
| 93 | + output.append("=" * 50) |
| 94 | + |
| 95 | + # 基础信息 |
| 96 | + output.append(f"归属地: {ip_info.get('归属地', '')}") |
| 97 | + output.append(f"运营商: {ip_info.get('运营商', '')}") |
| 98 | + |
| 99 | + # 详细信息 |
| 100 | + output.append("\n详细信息:") |
| 101 | + output.append("-" * 30) |
| 102 | + basic_info = ip_info.get('基本信息', {}) |
| 103 | + for key, value in basic_info.items(): |
| 104 | + if value: # 只显示有值的信息 |
| 105 | + output.append(f"{key}: {value}") |
| 106 | + |
| 107 | + # 街道定位历史 |
| 108 | + output.append("\n街道定位历史:") |
| 109 | + output.append("-" * 30) |
| 110 | + for history in ip_info.get('街道定位历史', []): |
| 111 | + output.append(history) |
| 112 | + |
| 113 | + # IP风险信息 |
| 114 | + output.append("\nIP风险信息:") |
| 115 | + output.append("-" * 30) |
| 116 | + risk_info = ip_info.get('IP风险信息', {}) |
| 117 | + for key, value in risk_info.items(): |
| 118 | + if value or str(value) == "0": # 显示有值或为0的字段 |
| 119 | + output.append(f"{key}: {value}") |
| 120 | + |
| 121 | + output.append("=" * 50) |
| 122 | + return "\n".join(output) |
| 123 | + |
| 124 | +def main(): |
| 125 | + """主函数""" |
| 126 | + print("高精度IP查询工具箱") |
| 127 | + print("-" * 30) |
| 128 | + |
| 129 | + while True: |
| 130 | + ip = input("请输入要查询的IP地址(输入'quit'退出): ").strip() |
| 131 | + |
| 132 | + if ip.lower() in ['quit', 'exit', 'q']: |
| 133 | + print("感谢使用,再见!") |
| 134 | + break |
| 135 | + |
| 136 | + if not ip: |
| 137 | + print("IP地址不能为空,请重新输入!") |
| 138 | + continue |
| 139 | + |
| 140 | + print(f"\n正在查询IP: {ip}") |
| 141 | + print("查询中,请稍候...") |
| 142 | + |
| 143 | + result = query_ip_info(ip) |
| 144 | + formatted_result = format_ip_info(result) |
| 145 | + |
| 146 | + print(f"\n{formatted_result}\n") |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
0 commit comments