-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_geolocation.py
More file actions
executable file
·280 lines (242 loc) · 8.84 KB
/
ip_geolocation.py
File metadata and controls
executable file
·280 lines (242 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
IP地理位置解析服务
该脚本用于解析MySQL数据库中存储的IP地址的地理位置信息,
并将解析结果(国家、城市、经纬度)存储回数据库。
只解析IPv4地址,忽略IPv6地址。
"""
import time
import logging
import ipaddress
import mysql.connector
import requests
from datetime import datetime
import sys
# 配置日志记录
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("ip_geolocation.log"),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger("IPGeolocation")
# 数据库配置
DB_CONFIG = {
'host': '42.194.236.54',
'user': 'root',
'password': 'As984315#',
'database': 'ipfs_nodes'
}
# IP地理位置解析API配置
IP_API_URL = "http://ip-api.com/json/{}"
BATCH_SIZE = 100 # 每次处理的IP数量
SLEEP_TIME = 60 # 处理完一批IP后的等待时间(秒)
API_RATE_LIMIT = 45 # IP-API免费版每分钟限制查询次数
QUERY_INTERVAL = 60 / API_RATE_LIMIT # 每次查询的间隔时间(秒)
def is_valid_ipv4(ip_str):
"""
检查是否为有效的IPv4地址
"""
try:
ip = ipaddress.ip_address(ip_str)
return ip.version == 4 and not ip.is_private and not ip.is_loopback and not ip.is_link_local
except ValueError:
return False
def ensure_geo_columns_exist():
"""
确保数据库表中存在地理位置信息的列
"""
try:
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
# 检查是否存在这些列
cursor.execute("""
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = %s AND TABLE_NAME = 'node_addresses'
AND COLUMN_NAME IN ('country', 'city', 'latitude', 'longitude', 'region', 'timezone', 'isp', 'geo_updated_at')
""", (DB_CONFIG['database'],))
existing_columns = [col[0] for col in cursor.fetchall()]
# 需要添加的列
columns_to_add = []
if 'country' not in existing_columns:
columns_to_add.append("ADD COLUMN `country` VARCHAR(100) NULL")
if 'city' not in existing_columns:
columns_to_add.append("ADD COLUMN `city` VARCHAR(100) NULL")
if 'latitude' not in existing_columns:
columns_to_add.append("ADD COLUMN `latitude` DECIMAL(10, 6) NULL")
if 'longitude' not in existing_columns:
columns_to_add.append("ADD COLUMN `longitude` DECIMAL(10, 6) NULL")
if 'region' not in existing_columns:
columns_to_add.append("ADD COLUMN `region` VARCHAR(100) NULL")
if 'timezone' not in existing_columns:
columns_to_add.append("ADD COLUMN `timezone` VARCHAR(50) NULL")
if 'isp' not in existing_columns:
columns_to_add.append("ADD COLUMN `isp` VARCHAR(255) NULL")
if 'geo_updated_at' not in existing_columns:
columns_to_add.append("ADD COLUMN `geo_updated_at` TIMESTAMP NULL")
# 如果有列需要添加,则执行ALTER TABLE命令
if columns_to_add:
alter_query = "ALTER TABLE node_addresses " + ", ".join(columns_to_add)
cursor.execute(alter_query)
conn.commit()
logger.info(f"Added {len(columns_to_add)} new columns to node_addresses table")
cursor.close()
conn.close()
return True
except Exception as e:
logger.error(f"Error ensuring geo columns exist: {e}")
return False
def get_unprocessed_ips(limit=BATCH_SIZE):
"""
获取未处理的IPv4地址
"""
try:
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
cursor.execute("""
SELECT id, host FROM node_addresses
WHERE geo_updated_at IS NULL
LIMIT %s
""", (limit,))
result = cursor.fetchall()
cursor.close()
conn.close()
# 过滤出有效的IPv4地址
valid_ips = []
for row_id, ip in result:
if is_valid_ipv4(ip):
valid_ips.append((row_id, ip))
return valid_ips
except Exception as e:
logger.error(f"Error getting unprocessed IPs: {e}")
return []
def get_ip_geolocation(ip):
"""
使用ip-api.com获取IP地址的地理位置信息
"""
try:
response = requests.get(IP_API_URL.format(ip), timeout=5)
data = response.json()
if data.get('status') == 'success':
return {
'country': data.get('country'),
'city': data.get('city'),
'latitude': data.get('lat'),
'longitude': data.get('lon'),
'region': data.get('regionName'),
'timezone': data.get('timezone'),
'isp': data.get('isp')
}
else:
logger.warning(f"Failed to get geolocation for IP {ip}: {data.get('message', 'Unknown error')}")
return None
except Exception as e:
logger.error(f"Error getting geolocation for IP {ip}: {e}")
return None
def update_ip_geolocation(row_id, geo_data):
"""
更新IP地址的地理位置信息到数据库
"""
try:
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
update_query = """
UPDATE node_addresses
SET country = %s, city = %s, latitude = %s, longitude = %s,
region = %s, timezone = %s, isp = %s, geo_updated_at = %s
WHERE id = %s
"""
cursor.execute(update_query, (
geo_data.get('country'),
geo_data.get('city'),
geo_data.get('latitude'),
geo_data.get('longitude'),
geo_data.get('region'),
geo_data.get('timezone'),
geo_data.get('isp'),
datetime.now(),
row_id
))
conn.commit()
cursor.close()
conn.close()
return True
except Exception as e:
logger.error(f"Error updating geolocation for row {row_id}: {e}")
return False
def mark_invalid_ip(row_id):
"""
标记无效的IP地址为已处理
"""
try:
conn = mysql.connector.connect(**DB_CONFIG)
cursor = conn.cursor()
update_query = """
UPDATE node_addresses
SET geo_updated_at = %s
WHERE id = %s
"""
cursor.execute(update_query, (datetime.now(), row_id))
conn.commit()
cursor.close()
conn.close()
return True
except Exception as e:
logger.error(f"Error marking invalid IP for row {row_id}: {e}")
return False
def process_batch():
"""
处理一批IP地址
"""
ips = get_unprocessed_ips()
if not ips:
logger.info("No unprocessed IPs found.")
return 0
logger.info(f"Processing {len(ips)} IP addresses")
processed_count = 0
for row_id, ip in ips:
try:
geo_data = get_ip_geolocation(ip)
if geo_data:
if update_ip_geolocation(row_id, geo_data):
logger.info(f"Updated geolocation for IP {ip} (Row ID: {row_id})")
processed_count += 1
else:
# 即使无法获取地理位置,也标记为已处理,避免反复尝试
mark_invalid_ip(row_id)
logger.warning(f"Marked IP {ip} as processed (Row ID: {row_id})")
# 遵守API请求频率限制
time.sleep(QUERY_INTERVAL)
except Exception as e:
logger.error(f"Error processing IP {ip} (Row ID: {row_id}): {e}")
return processed_count
def main():
"""
主函数
"""
logger.info("Starting IP Geolocation Service")
# 确保地理位置列存在
if not ensure_geo_columns_exist():
logger.error("Failed to ensure geo columns exist. Exiting.")
return
# 主循环
while True:
try:
processed_count = process_batch()
logger.info(f"Processed {processed_count} IP addresses")
# 如果没有处理任何记录,可能是所有IP都已处理,等待更长时间
if processed_count == 0:
logger.info(f"No IP addresses processed. Waiting for {SLEEP_TIME * 5} seconds.")
time.sleep(SLEEP_TIME * 5)
else:
logger.info(f"Waiting for {SLEEP_TIME} seconds before next batch.")
time.sleep(SLEEP_TIME)
except Exception as e:
logger.error(f"Error in main loop: {e}")
logger.info(f"Retrying in {SLEEP_TIME} seconds.")
time.sleep(SLEEP_TIME)
if __name__ == "__main__":
main()