dan92 commited on
Commit
246ea36
·
verified ·
1 Parent(s): 1cb3f25

Delete proxy_pool.py

Browse files
Files changed (1) hide show
  1. proxy_pool.py +0 -175
proxy_pool.py DELETED
@@ -1,175 +0,0 @@
1
- import requests
2
- import time
3
- import logging
4
- import random
5
- from threading import Lock, RLock
6
- from collections import defaultdict
7
- from datetime import datetime, timedelta
8
-
9
- logger = logging.getLogger(__name__)
10
-
11
- class ProxyPool:
12
- def __init__(self):
13
- self.proxies = []
14
- self.proxy_usage = defaultdict(list)
15
- self.lock = RLock()
16
- self.last_fetch_time = datetime.min
17
- self.fetch_interval = 300
18
- self.last_cleanup_time = time.time()
19
- self.cleanup_interval = 60
20
-
21
- def _cleanup_usage_records(self):
22
- """清理过期的使用记录"""
23
- current_time = time.time()
24
- if current_time - self.last_cleanup_time < self.cleanup_interval:
25
- return
26
-
27
- with self.lock:
28
- for ip in list(self.proxy_usage.keys()):
29
- self.proxy_usage[ip] = [t for t in self.proxy_usage[ip]
30
- if current_time - t < 1.0]
31
- if not self.proxy_usage[ip]:
32
- del self.proxy_usage[ip]
33
- self.last_cleanup_time = current_time
34
-
35
- def _parse_proxy_list(self, text):
36
- """解析代理列表文本"""
37
- proxies = []
38
- try:
39
- lines = [line.strip() for line in text.strip().split(',') if line.strip()]
40
-
41
- for line in lines:
42
- try:
43
- if line.startswith('http://'):
44
- proxy_type = 'http'
45
- proxy = line[7:]
46
- elif line.startswith('socks4://'):
47
- proxy_type = 'socks4'
48
- proxy = line[9:]
49
- elif line.startswith('socks5://'):
50
- proxy_type = 'socks5'
51
- proxy = line[9:]
52
- else:
53
- continue
54
-
55
- if ':' in proxy:
56
- ip, port = proxy.split(':')
57
- proxies.append({
58
- 'type': proxy_type,
59
- 'ip': ip.strip(),
60
- 'port': int(port.strip())
61
- })
62
- except Exception as e:
63
- logger.error(f"解析代理失败: {line}, 错误: {e}")
64
- continue
65
-
66
- except Exception as e:
67
- logger.error(f"解析代理列表失败: {e}")
68
- return proxies
69
-
70
- def _fetch_proxies(self):
71
- """从API获取代理列表"""
72
- try:
73
- urls = [
74
- "https://269900.xyz/fetch_http?count=5",
75
- "https://269900.xyz/fetch_https?count=5",
76
- "https://269900.xyz/fetch_socks5?count=5"
77
- ]
78
-
79
- new_proxies = []
80
- for url in urls:
81
- try:
82
- response = requests.get(url, timeout=5)
83
- if response.status_code == 200:
84
- proxies = self._parse_proxy_list(response.text)
85
- new_proxies.extend(proxies)
86
- logger.info(f"成功获取 {len(proxies)} 个代理从 {url}")
87
- except requests.RequestException as e:
88
- logger.error(f"获取代理失败 ({url}): {e}")
89
- continue
90
-
91
- return new_proxies
92
- except Exception as e:
93
- logger.error(f"获取代理失败: {e}")
94
- return []
95
-
96
- def update_proxy_pool(self):
97
- """更新代理池"""
98
- current_time = datetime.now()
99
- should_update = False
100
-
101
- with self.lock:
102
- if (current_time - self.last_fetch_time).total_seconds() >= self.fetch_interval:
103
- should_update = True
104
- self.last_fetch_time = current_time
105
-
106
- if should_update:
107
- new_proxies = self._fetch_proxies()
108
- if new_proxies:
109
- with self.lock:
110
- self.proxies = new_proxies
111
- logger.info(f"代理池更新成功,当前代理数量: {len(self.proxies)}")
112
-
113
- def can_use_proxy(self, proxy_ip):
114
- """检查代理IP是否可以使用"""
115
- current_time = time.time()
116
-
117
- with self.lock:
118
- usage_times = self.proxy_usage[proxy_ip]
119
- usage_times = [t for t in usage_times if current_time - t < 1.0]
120
- self.proxy_usage[proxy_ip] = usage_times
121
-
122
- if len(usage_times) >= 5:
123
- return False
124
-
125
- self.proxy_usage[proxy_ip].append(current_time)
126
- return True
127
-
128
- def get_proxy(self):
129
- """获取一个可用的代理"""
130
- self._cleanup_usage_records()
131
- self.update_proxy_pool()
132
-
133
- for _ in range(3):
134
- with self.lock:
135
- if not self.proxies:
136
- return None
137
-
138
- available_proxies = []
139
- for proxy in self.proxies:
140
- proxy_ip = proxy.get('ip')
141
- if proxy_ip and self.can_use_proxy(proxy_ip):
142
- available_proxies.append(proxy)
143
-
144
- if available_proxies:
145
- proxy = random.choice(available_proxies)
146
- proxy_type = proxy.get('type', 'http').lower()
147
- proxy_str = f"{proxy_type}://{proxy['ip']}:{proxy['port']}"
148
- return {
149
- 'http': proxy_str,
150
- 'https': proxy_str if proxy_type == 'http' else None
151
- }
152
-
153
- time.sleep(0.1)
154
-
155
- return None
156
-
157
- def remove_proxy(self, proxy):
158
- """移除无效的代理"""
159
- if not proxy:
160
- return
161
-
162
- with self.lock:
163
- try:
164
- proxy_str = proxy.get('http') or proxy.get('https')
165
- if not proxy_str:
166
- return
167
-
168
- for p in self.proxies[:]:
169
- current_proxy_str = f"{p['type']}://{p['ip']}:{p['port']}"
170
- if current_proxy_str == proxy_str:
171
- self.proxies.remove(p)
172
- logger.info(f"移除无效代理: {proxy_str}")
173
- break
174
- except Exception as e:
175
- logger.error(f"移除代理时出错: {e}")