mns-common 1.3.9.8__py3-none-any.whl → 1.4.0.0__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,34 @@ 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()
68
+ # 加上分钟
53
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
-
57
72
  # 获取10分钟动态ip
58
- ip = liu_guan_proxy_api.get_proxy_api(order_id, secret, str(60 * minutes))
59
-
73
+ ip = generate_proxy_ip_api(minutes)
60
74
  result_dict = {"_id": ip,
61
75
  'effect_time': str_now_date,
62
76
  'ip': ip}
@@ -78,10 +92,95 @@ def get_proxy_ip(minutes):
78
92
  return generate_proxy_ip(minutes)
79
93
 
80
94
 
81
- if __name__ == '__main__':
82
- generate_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)
95
+ def check_baidu_proxy(proxy_ip, timeout=2):
96
+ """
97
+ 检测代理IP是否能访问百度
98
+ :param proxy_ip: 代理IP地址
99
+ :param proxy_port: 代理端口
100
+ :param timeout: 超时时间()
101
+ :return: (是否可用, 响应时间, 检测结果信息)
102
+ """
103
+ # 构造代理地址
104
+
105
+ # 设置代理参数
106
+ proxies = {
107
+ "http": proxy_ip,
108
+ "https": proxy_ip
109
+ }
110
+
111
+ # 模拟浏览器请求头
112
+ headers = {
113
+ "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",
114
+ "Accept-Language": "zh-CN,zh;q=0.9",
115
+ "Connection": "keep-alive"
116
+ }
117
+
118
+ try:
119
+ # 记录开始时间
120
+ start_time = time.time()
121
+
122
+ # 发送请求到百度
123
+ response = requests.get(
124
+ url="https://www.baidu.com",
125
+ proxies=proxies,
126
+ headers=headers,
127
+ timeout=timeout,
128
+ allow_redirects=True # 允许重定向
129
+ )
130
+
131
+ # 计算响应时间
132
+ response_time = round((time.time() - start_time) * 1000) # 毫秒
133
+ # 检查响应状态和内容
134
+ if response.status_code == 200:
135
+ # 验证是否返回百度页面
136
+ if "百度一下" in response.text and "baidu.com" in response.text:
137
+ logger.info("代理ip可用:{},响应时间:{}", proxy_ip, response_time)
138
+ return True
139
+ else:
140
+ logger.error("代理ip不可用:{},响应时间:{}", proxy_ip, response_time)
141
+ return False
142
+ else:
143
+ logger.error("代理ip不可用:{},响应时间:{},HTTP状态码异常:{}", proxy_ip, response_time, response.status_code)
144
+ return False
145
+ except requests.exceptions.ConnectTimeout:
146
+ logger.error("代理ip不可用:{},连接超时", proxy_ip, response_time)
147
+ return False
148
+ except requests.exceptions.ProxyError:
149
+ logger.error("代理ip不可用:{},代理拒绝连接", proxy_ip, response_time)
150
+ return False
151
+ except requests.exceptions.SSLError:
152
+ logger.error("代理ip不可用:{},SSL证书错误", proxy_ip, response_time)
153
+ return False
154
+ except requests.exceptions.RequestException as e:
155
+ logger.error("代理ip不可用:{},网络错误:{}", proxy_ip, str(e))
156
+ return False
157
+
158
+
159
+ def check_proxy(proxy_ip, timeout=2):
160
+ proxies = {
161
+ "http": proxy_ip,
162
+ "https": proxy_ip
163
+ }
164
+ try:
165
+ # 测试请求(httpbin.org 返回请求的IP)
166
+ response = requests.get(
167
+ "http://httpbin.org/ip",
168
+ proxies=proxies,
169
+ timeout=timeout # 超时时间
170
+ )
171
+ if response.status_code == 200:
172
+ return True
173
+ else:
174
+ logger.error("代理ip不可用:{}", proxy_ip)
175
+ return False
176
+ except Exception as e:
177
+ logger.error("代理ip不可用:{},{}", proxy_ip, e)
178
+ return False
179
+
180
+
181
+ if __name__ == "__main__":
182
+ target_ip = "112.28.228.67:35528" # Google DNS
183
+ if check_proxy(target_ip, 2):
184
+ print(f"{target_ip} 可以访问")
185
+ else:
186
+ print(f"{target_ip} 无法访问")
@@ -1,4 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mns-common
3
- Version: 1.3.9.8
3
+ Version: 1.4.0.0
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=TqHdbJM4e6Y5aHSsDB8wYXtcaYIbqjrOn9mxrLtbC_w,2763
116
+ mns_common/component/proxies/proxy_common_api.py,sha256=kzJ0OWWQVNte-SiV_OwEABG6KPX4FaZnZK42bHWMJGI,6075
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.8.dist-info/METADATA,sha256=58B8LAPT3KMbtKZqoNSVH6EyONeCZotHdbCD3kJ6hmg,61
156
- mns_common-1.3.9.8.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
157
- mns_common-1.3.9.8.dist-info/top_level.txt,sha256=ZC58kAR-8Hvc6U2xhYNBNLAh3mb6sZazbdj5nZpvEkQ,11
158
- mns_common-1.3.9.8.dist-info/RECORD,,
155
+ mns_common-1.4.0.0.dist-info/METADATA,sha256=bt62Ih8aZdr44Lwy257y40-UZ-WvobYc_oY0Y0P8n1E,61
156
+ mns_common-1.4.0.0.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
157
+ mns_common-1.4.0.0.dist-info/top_level.txt,sha256=ZC58kAR-8Hvc6U2xhYNBNLAh3mb6sZazbdj5nZpvEkQ,11
158
+ mns_common-1.4.0.0.dist-info/RECORD,,