mns-common 1.3.9.7__py3-none-any.whl → 1.3.9.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mns-common might be problematic. Click here for more details.

@@ -12,6 +12,10 @@ import mns_common.utils.data_frame_util as data_frame_util
12
12
  from mns_common.db.MongodbUtil import MongodbUtil
13
13
  import mns_common.constant.db_name_constant as db_name_constant
14
14
  import datetime
15
+ import requests
16
+ import time
17
+ from loguru import logger
18
+ from functools import lru_cache
15
19
 
16
20
  mongodb_util = MongodbUtil('27017')
17
21
 
@@ -39,24 +43,40 @@ def check_valid(ip_proxy_pool):
39
43
  return False
40
44
 
41
45
 
46
+ @lru_cache(maxsize=None)
47
+ def get_account_cache():
48
+ query = {"type": "liu_guan_proxy", }
49
+ return mongodb_util.find_query_data(db_name_constant.STOCK_ACCOUNT_INFO, query)
50
+
51
+
52
+ def generate_proxy_ip_api(minutes):
53
+ stock_account_info = get_account_cache()
54
+ order_id = list(stock_account_info['password'])[0]
55
+ secret = list(stock_account_info['account'])[0]
56
+ # 获取10分钟动态ip
57
+ ip = liu_guan_proxy_api.get_proxy_api(order_id, secret, str(60 * minutes))
58
+ return ip
59
+
60
+
42
61
  def generate_proxy_ip(minutes):
43
62
  ip_proxy_pool = mongodb_util.find_all_data(db_name_constant.IP_PROXY_POOL)
44
63
  if data_frame_util.is_not_empty(ip_proxy_pool):
45
64
  return list(ip_proxy_pool['ip'])[0]
46
65
  else:
47
- query = {"type": "liu_guan_proxy", }
48
- stock_account_info = mongodb_util.find_query_data(db_name_constant.STOCK_ACCOUNT_INFO, query)
49
- order_id = list(stock_account_info['password'])[0]
50
- secret = list(stock_account_info['account'])[0]
51
-
66
+ remove_proxy_ip()
52
67
  now_date = datetime.datetime.now()
53
- time_to_add = datetime.timedelta(minutes=minutes - 1, seconds=30)
68
+ # 加上分钟
69
+ time_to_add = datetime.timedelta(minutes=minutes)
54
70
  new_date = now_date + time_to_add
55
71
  str_now_date = new_date.strftime('%Y-%m-%d %H:%M:%S')
56
72
 
57
73
  # 获取10分钟动态ip
58
- ip = liu_guan_proxy_api.get_proxy_api(order_id, secret, str(60 * minutes))
59
-
74
+ while True:
75
+ ip = generate_proxy_ip_api(minutes)
76
+ if check_proxy(ip, timeout=2):
77
+ break
78
+ else:
79
+ time.sleep(0.5)
60
80
  result_dict = {"_id": ip,
61
81
  'effect_time': str_now_date,
62
82
  'ip': ip}
@@ -67,21 +87,106 @@ def generate_proxy_ip(minutes):
67
87
  return ip
68
88
 
69
89
 
70
- def get_proxy_ip( minutes):
90
+ def get_proxy_ip(minutes):
71
91
  ip_proxy_pool = query_liu_guan_proxy_ip()
72
92
  if data_frame_util.is_empty(ip_proxy_pool):
73
93
  return generate_proxy_ip(minutes)
74
94
  else:
75
- if check_valid( ip_proxy_pool):
95
+ if check_valid(ip_proxy_pool):
76
96
  return list(ip_proxy_pool['ip'])[0]
77
97
  else:
78
98
  return generate_proxy_ip(minutes)
79
99
 
80
100
 
81
- if __name__ == '__main__':
82
- get_proxy_ip(1)
83
- while True:
84
- now_date_test = datetime.datetime.now()
85
- str_now_date_test = now_date_test.strftime('%Y-%m-%d %H:%M:%S')
86
- ip_test = get_proxy_ip(str_now_date_test)
87
- print(ip_test)
101
+ def check_baidu_proxy(proxy_ip, timeout=2):
102
+ """
103
+ 检测代理IP是否能访问百度
104
+ :param proxy_ip: 代理IP地址
105
+ :param proxy_port: 代理端口
106
+ :param timeout: 超时时间()
107
+ :return: (是否可用, 响应时间, 检测结果信息)
108
+ """
109
+ # 构造代理地址
110
+
111
+ # 设置代理参数
112
+ proxies = {
113
+ "http": proxy_ip,
114
+ "https": proxy_ip
115
+ }
116
+
117
+ # 模拟浏览器请求头
118
+ headers = {
119
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
120
+ "Accept-Language": "zh-CN,zh;q=0.9",
121
+ "Connection": "keep-alive"
122
+ }
123
+
124
+ try:
125
+ # 记录开始时间
126
+ start_time = time.time()
127
+
128
+ # 发送请求到百度
129
+ response = requests.get(
130
+ url="https://www.baidu.com",
131
+ proxies=proxies,
132
+ headers=headers,
133
+ timeout=timeout,
134
+ allow_redirects=True # 允许重定向
135
+ )
136
+
137
+ # 计算响应时间
138
+ response_time = round((time.time() - start_time) * 1000) # 毫秒
139
+ # 检查响应状态和内容
140
+ if response.status_code == 200:
141
+ # 验证是否返回百度页面
142
+ if "百度一下" in response.text and "baidu.com" in response.text:
143
+ logger.info("代理ip可用:{},响应时间:{}", proxy_ip, response_time)
144
+ return True
145
+ else:
146
+ logger.error("代理ip不可用:{},响应时间:{}", proxy_ip, response_time)
147
+ return False
148
+ else:
149
+ logger.error("代理ip不可用:{},响应时间:{},HTTP状态码异常:{}", proxy_ip, response_time, response.status_code)
150
+ return False
151
+ except requests.exceptions.ConnectTimeout:
152
+ logger.error("代理ip不可用:{},连接超时", proxy_ip, response_time)
153
+ return False
154
+ except requests.exceptions.ProxyError:
155
+ logger.error("代理ip不可用:{},代理拒绝连接", proxy_ip, response_time)
156
+ return False
157
+ except requests.exceptions.SSLError:
158
+ logger.error("代理ip不可用:{},SSL证书错误", proxy_ip, response_time)
159
+ return False
160
+ except requests.exceptions.RequestException as e:
161
+ logger.error("代理ip不可用:{},网络错误:{}", proxy_ip, str(e))
162
+ return False
163
+
164
+
165
+ def check_proxy(proxy_ip, timeout=2):
166
+ proxies = {
167
+ "http": proxy_ip,
168
+ "https": proxy_ip
169
+ }
170
+ try:
171
+ # 测试请求(httpbin.org 返回请求的IP)
172
+ response = requests.get(
173
+ "http://httpbin.org/ip",
174
+ proxies=proxies,
175
+ timeout=timeout # 超时时间
176
+ )
177
+ if response.status_code == 200:
178
+ return True
179
+ else:
180
+ logger.error("代理ip不可用:{}", proxy_ip)
181
+ return False
182
+ except Exception as e:
183
+ logger.error("代理ip不可用:{},{}", proxy_ip, e)
184
+ return False
185
+
186
+
187
+ if __name__ == "__main__":
188
+ target_ip = "112.28.228.67:35528" # Google DNS
189
+ if check_proxy(target_ip, 2):
190
+ print(f"{target_ip} 可以访问")
191
+ else:
192
+ print(f"{target_ip} 无法访问")
@@ -1,4 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mns-common
3
- Version: 1.3.9.7
3
+ Version: 1.3.9.9
4
4
 
@@ -113,7 +113,7 @@ mns_common/component/k_line/patterns/pattern_Enum.py,sha256=bl8cH1H3BWdj_deVO124
113
113
  mns_common/component/price/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
114
114
  mns_common/component/price/trade_price_service_api.py,sha256=0loBjbOt__o-ngc2Q4n5lF8_0x2WINRpL-cH1341Uaw,4396
115
115
  mns_common/component/proxies/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
116
- mns_common/component/proxies/proxy_common_api.py,sha256=8wluf_DcXw6098mflBdYg-hMJs2is0mmQ3dUhVH1wRE,2766
116
+ mns_common/component/proxies/proxy_common_api.py,sha256=wNyqGFo1KnInY5E4bk83CQoXP0K3vQDDs5rPRTM-O7U,6221
117
117
  mns_common/component/qmt/__init__.py,sha256=wEg73KlZo-dU0yKGwpA1C2y6LZm4IBb94tNda1tqLeg,163
118
118
  mns_common/component/qmt/qmt_buy_service.py,sha256=tLTgrSxCcxuMhADRBBrW4ZWR_3MdbMZvvMdH5hbwyJU,7190
119
119
  mns_common/component/real_time/__init__.py,sha256=2U9DiKslxsWwLLEcZKjS8UiQPN1QgALvnK3HiJNIZE0,163
@@ -152,7 +152,7 @@ mns_common/utils/date_handle_util.py,sha256=XS-MyA8_7k35LOCFAYOHgVcVkMft_Kc4Wa9U
152
152
  mns_common/utils/db_util.py,sha256=hSmfNAN4vEeEaUva6_cicZEhb2jSnib-Gvk2reke1vc,2590
153
153
  mns_common/utils/file_util.py,sha256=egWu6PenGPRp_ixrNTHKarT4dAnOT6FETR82EHUZJnQ,1042
154
154
  mns_common/utils/ip_util.py,sha256=UTcYfz_uytB__6nlBf7T-izuI7hi4XdB6ET0sJgEel4,969
155
- mns_common-1.3.9.7.dist-info/METADATA,sha256=kB0ngpQ2wrfVqyNIsTeavDDi2K6RP8DDciBgkl6sAl4,61
156
- mns_common-1.3.9.7.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
157
- mns_common-1.3.9.7.dist-info/top_level.txt,sha256=ZC58kAR-8Hvc6U2xhYNBNLAh3mb6sZazbdj5nZpvEkQ,11
158
- mns_common-1.3.9.7.dist-info/RECORD,,
155
+ mns_common-1.3.9.9.dist-info/METADATA,sha256=4kf1Xk0G1IQfxzQd2nWHhhOgjlyYLQnl0EwPzlpH0Nc,61
156
+ mns_common-1.3.9.9.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
157
+ mns_common-1.3.9.9.dist-info/top_level.txt,sha256=ZC58kAR-8Hvc6U2xhYNBNLAh3mb6sZazbdj5nZpvEkQ,11
158
+ mns_common-1.3.9.9.dist-info/RECORD,,