dan92 commited on
Commit
9ed1a6b
·
verified ·
1 Parent(s): 89e9525

Upload proxy_pool.py

Browse files
Files changed (1) hide show
  1. proxy_pool.py +38 -3
proxy_pool.py CHANGED
@@ -16,6 +16,40 @@ class ProxyPool:
16
  self.last_fetch_time = datetime.min
17
  self.fetch_interval = 300 # 5分钟更新一次代理池
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def _fetch_proxies(self):
20
  """从API获取代理列表"""
21
  try:
@@ -29,8 +63,9 @@ class ProxyPool:
29
  for url in urls:
30
  response = requests.get(url, timeout=10)
31
  if response.status_code == 200:
32
- proxies = response.json()
33
  new_proxies.extend(proxies)
 
34
 
35
  return new_proxies
36
  except Exception as e:
@@ -82,7 +117,7 @@ class ProxyPool:
82
  proxy_str = f"{proxy_type}://{proxy_ip}:{proxy.get('port')}"
83
  return {
84
  'http': proxy_str,
85
- 'https': proxy_str
86
  }
87
 
88
  return None
@@ -92,4 +127,4 @@ class ProxyPool:
92
  with self.lock:
93
  if proxy in self.proxies:
94
  self.proxies.remove(proxy)
95
- logger.info(f"移除无效代理: {proxy}")
 
16
  self.last_fetch_time = datetime.min
17
  self.fetch_interval = 300 # 5分钟更新一次代理池
18
 
19
+ def _parse_proxy_list(self, text):
20
+ """解析代理列表文本"""
21
+ proxies = []
22
+ for line in text.strip().split(','):
23
+ line = line.strip()
24
+ if not line:
25
+ continue
26
+
27
+ try:
28
+ if line.startswith('http://'):
29
+ proxy_type = 'http'
30
+ proxy = line[7:]
31
+ elif line.startswith('socks4://'):
32
+ proxy_type = 'socks4'
33
+ proxy = line[9:]
34
+ elif line.startswith('socks5://'):
35
+ proxy_type = 'socks5'
36
+ proxy = line[9:]
37
+ else:
38
+ continue
39
+
40
+ if ':' in proxy:
41
+ ip, port = proxy.split(':')
42
+ proxies.append({
43
+ 'type': proxy_type,
44
+ 'ip': ip,
45
+ 'port': int(port)
46
+ })
47
+ except Exception as e:
48
+ logger.error(f"解析代理失败: {line}, 错误: {e}")
49
+ continue
50
+
51
+ return proxies
52
+
53
  def _fetch_proxies(self):
54
  """从API获取代理列表"""
55
  try:
 
63
  for url in urls:
64
  response = requests.get(url, timeout=10)
65
  if response.status_code == 200:
66
+ proxies = self._parse_proxy_list(response.text)
67
  new_proxies.extend(proxies)
68
+ logger.info(f"成功获取 {len(proxies)} 个代理从 {url}")
69
 
70
  return new_proxies
71
  except Exception as e:
 
117
  proxy_str = f"{proxy_type}://{proxy_ip}:{proxy.get('port')}"
118
  return {
119
  'http': proxy_str,
120
+ 'https': proxy_str if proxy_type == 'http' else None
121
  }
122
 
123
  return None
 
127
  with self.lock:
128
  if proxy in self.proxies:
129
  self.proxies.remove(proxy)
130
+ logger.info(f"移除无效代理: {proxy}")