xtn-tools-pro 1.0.0.2.7__py3-none-any.whl → 1.0.0.2.9__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,36 +8,38 @@
8
8
  # --------------------------------------------------------------------------------------------------
9
9
  # 2025/1/14 xiatn V00.01.000 新建
10
10
  # --------------------------------------------------------------------------------------------------
11
- import os
12
- import configparser
13
- from xtn_tools_pro.utils.file_utils import check_file_exists
11
+ import time
12
+ import inspect
13
+ import requests
14
+ import threading
15
+ import multiprocessing
16
+ import concurrent.futures
17
+ from multiprocessing import Process
14
18
 
15
19
 
16
20
  class GoFun:
17
- def __init__(self, ini_dict):
21
+ def __init__(self, ini_dict, logger, go_task_function=None):
18
22
  # 读取配置信息
19
23
  host = ini_dict.get('host', '')
20
24
  port = ini_dict.get('port', 0)
21
25
  task = ini_dict.get('task', '')
22
26
  auto = ini_dict.get('auto', '')
23
- is_processes = ini_dict.get('is_processes', False)
24
27
  processes_num = ini_dict.get('processes_num', 0)
25
- is_thread = ini_dict.get('is_thread', False)
26
28
  thread_num = ini_dict.get('thread_num', 0)
29
+ restart_time = ini_dict.get('restart_time', 0)
27
30
 
28
31
  self.__ini_info = {
29
32
  "host": host,
30
33
  "port": port,
31
34
  "task": task,
32
35
  "auto": auto,
33
- "is_processes": is_processes,
34
36
  "processes_num": processes_num,
35
- "is_thread": is_thread,
36
37
  "thread_num": thread_num,
38
+ "restart_time": restart_time,
37
39
  }
38
40
 
39
41
  for server_k, server_v in self.__ini_info.items():
40
- if not server_v and server_k not in ["port", "is_processes", "processes_num", "is_thread", "thread_num"]:
42
+ if not server_v and server_k not in ["port", "processes_num", "thread_num", "restart_time"]:
41
43
  raise Exception(f"ini_dict 配置 {server_k} 不存在")
42
44
 
43
45
  if port:
@@ -47,8 +49,181 @@ class GoFun:
47
49
 
48
50
  download_url = task_host + "/filter_server/phone/get"
49
51
  upload_url = task_host + "/filter_server/phone/update"
52
+ external_ip = self.__get_external_ip()
50
53
 
51
54
  self.__ini_info["download_url"] = download_url
52
55
  self.__ini_info["upload_url"] = upload_url
56
+ self.__ini_info["external_ip"] = external_ip
53
57
 
54
- print(self.__ini_info)
58
+ self.logger = logger
59
+
60
+ if not go_task_function:
61
+ return
62
+
63
+ # 共享任务队列
64
+ download_queue = multiprocessing.Queue()
65
+ upload_queue = multiprocessing.Queue()
66
+ download_task_process = multiprocessing.Process(target=self._download_and_upload_task,
67
+ args=(download_queue, upload_queue, self.__ini_info, logger))
68
+ download_task_process.start()
69
+
70
+ # 根据配置启动任务
71
+ if go_task_function:
72
+ go_task_fun_process = multiprocessing.Process(target=self._go_task_fun_task,
73
+ args=(download_queue, upload_queue, self.__ini_info,
74
+ go_task_function, logger))
75
+ go_task_fun_process.start()
76
+
77
+ def __get_external_ip(self):
78
+ """
79
+ 获取当前网络ip
80
+ :return:
81
+ """
82
+ while True:
83
+ try:
84
+ rp = requests.get('https://httpbin.org/ip')
85
+ rp_json = rp.json()
86
+ print(f"当前网络ip --> {rp_json}")
87
+ return rp_json['origin']
88
+ except Exception as e:
89
+ pass
90
+
91
+ def _download_and_upload_task(self, download_queue, upload_queue, ini_info, logger):
92
+ """
93
+ 使用两个线程 打开 获取任务、回写任务
94
+ :param queue:
95
+ :return:
96
+ """
97
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
98
+ caller_name = caller.function # 获取调用者的函数名
99
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
100
+ if caller_name != "run" or caller_class is None:
101
+ raise Exception("错误调用")
102
+ thread_download_task = threading.Thread(target=self.__download_task, args=(download_queue, ini_info, logger))
103
+ thread_download_task.start()
104
+ thread_upload_task = threading.Thread(target=self.__upload_task, args=(upload_queue, ini_info, logger))
105
+ thread_upload_task.start()
106
+
107
+ def __download_task(self, download_queue, ini_info, logger):
108
+ """
109
+ 获取任务
110
+ :param queue:
111
+ :return:
112
+ """
113
+ download_url = ini_info["download_url"]
114
+ external_ip = ini_info["external_ip"]
115
+ auto = ini_info["auto"]
116
+ task = ini_info["task"]
117
+ headers = {"Authorization": auto}
118
+ params = {"taskType": task}
119
+ while True:
120
+ qsize = download_queue.qsize()
121
+ logger.info(f"获取任务,当前队列任务数:{qsize}")
122
+ if download_queue.qsize() >= 10:
123
+ time.sleep(2)
124
+ continue
125
+ resp = requests.get(download_url, headers=headers, params=params, timeout=5)
126
+ json_data = resp.json()
127
+ result_list = json_data.get("result", [])
128
+ for task_item in result_list:
129
+ download_queue.put(task_item)
130
+ logger.info(f"成功获取任务个数:{len(result_list)}")
131
+
132
+ def __upload_task(self, queue, ini_info, logger):
133
+ """
134
+ 回写任务
135
+ :return:
136
+ """
137
+ upload_url = ini_info["upload_url"]
138
+ external_ip = ini_info["external_ip"]
139
+ auto = ini_info["auto"]
140
+ task = ini_info["task"]
141
+ headers = {"Authorization": auto}
142
+ params = {"taskType": task}
143
+ while True:
144
+ # 判断队列是否有值
145
+ empty = queue.empty()
146
+ if empty:
147
+ time.sleep(2)
148
+ continue
149
+
150
+ # 循环全部获取队列的任务
151
+ result_list = []
152
+ try:
153
+ while True:
154
+ result_list.append(queue.get_nowait())
155
+ except Exception as e:
156
+ pass
157
+
158
+ # 回写任务
159
+ data = {"result": result_list, "remoteAddr": external_ip}
160
+ while True:
161
+ try:
162
+ resp = requests.post(upload_url, json=data, headers=headers, params=params, timeout=5)
163
+ json_data = resp.json()
164
+ logger.info(f"成功回写任务个数:{len(result_list)},{json_data}")
165
+ break
166
+ except Exception as e:
167
+ logger.critical(f"回写异常:{e}")
168
+
169
+ def _go_task_fun_task(self, download_queue, upload_queue, ini_info, go_task_function, logger):
170
+ """
171
+ 单函数,根据配置启动程序
172
+ :param queue:
173
+ :return:
174
+ """
175
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
176
+ caller_name = caller.function # 获取调用者的函数名
177
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
178
+ if caller_name != "run" or caller_class is None:
179
+ raise Exception("错误调用")
180
+
181
+ processes_num = ini_info["processes_num"]
182
+ thread_num = ini_info["thread_num"]
183
+ restart_time = ini_info["restart_time"]
184
+
185
+ processes_num = 1 if processes_num <= 0 else processes_num
186
+ thread_num = 1 if thread_num <= 0 else thread_num
187
+
188
+ go_task_fun_cnt = 0
189
+ processes_start_list = []
190
+ while True:
191
+ try:
192
+ if not processes_start_list:
193
+ go_task_fun_cnt += 1
194
+ logger.info(
195
+ f"第{go_task_fun_cnt}次,进程数:{processes_num},线程数:{thread_num},等待{restart_time}秒强制下一次")
196
+ for i in range(processes_num):
197
+ p = Process(target=self._run_with_timeout,
198
+ args=(download_queue, upload_queue, thread_num, go_task_function))
199
+ processes_start_list.append(p)
200
+ p.start()
201
+
202
+ if not restart_time:
203
+ # 一直执行 不退出
204
+ continue
205
+
206
+ time.sleep(restart_time)
207
+ # 关闭所有进程
208
+ for p in processes_start_list:
209
+ p.terminate()
210
+ p.join() # 等待进程确实结束
211
+ processes_start_list = []
212
+
213
+ except Exception as e:
214
+ pass
215
+
216
+ def _run_with_timeout(self, download_queue, upload_queue, thread_num, go_task_function):
217
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
218
+ caller_name = caller.function # 获取调用者的函数名
219
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
220
+ if caller_name != "run" or caller_class is None:
221
+ raise Exception("错误调用")
222
+
223
+ with concurrent.futures.ThreadPoolExecutor(max_workers=thread_num) as executor:
224
+ # 提交10个函数到线程池中执行
225
+ futures = [executor.submit(go_task_function, download_queue, upload_queue) for _ in range(thread_num)]
226
+
227
+ # 等待所有线程完成
228
+ for future in concurrent.futures.as_completed(futures):
229
+ future.result()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.0.2.7
3
+ Version: 1.0.0.2.9
4
4
  Summary: xtn 开发工具
5
5
  Author: xtn
6
6
  Author-email: czw011122@gmail.com
@@ -10,7 +10,7 @@ xtn_tools_pro/proxy/XiaoXiangProxy.py,sha256=45SsMZDc-3Ta4THX38vScjaeRBJSp420AJw
10
10
  xtn_tools_pro/proxy/__init__.py,sha256=WRwh6s2lruMu5buh0ejo9EK54kWT_VQhCsFGNFAmcyo,418
11
11
  xtn_tools_pro/proxy/proxy.py,sha256=No6E1pFY5yx2F4976pXPrLtq-QEVp79KupzcufjSN58,8703
12
12
  xtn_tools_pro/task_pro/__init__.py,sha256=nK3U47hWwE1H875ieEToH9r-jzXHS-PXk8cDstOvRE8,418
13
- xtn_tools_pro/task_pro/go_fun.py,sha256=x6RxA7wsqTfhlzlieJ3Q-sj9jz_ALanEMWTXIzWDBGE,1950
13
+ xtn_tools_pro/task_pro/go_fun.py,sha256=sizIHXnFa_OQsYtdx5GJPH6rBAUbPCIIopdHqBHJEDI,9264
14
14
  xtn_tools_pro/utils/__init__.py,sha256=I1_n_NP23F2lBqlF4EOlnOdLYxM8M4pbn63UhJN1hRE,418
15
15
  xtn_tools_pro/utils/crypto.py,sha256=RZ5AET4udlraACWMeNF-17JiZ2R6Ahb47_j4tjkV7LE,3190
16
16
  xtn_tools_pro/utils/file_utils.py,sha256=VfdIxog4s1UW5NpKkCvQsUs9qHjLoNCnstZbnftkT4w,2046
@@ -19,8 +19,8 @@ xtn_tools_pro/utils/log.py,sha256=XZChGrKWgAz42o_gFi_d7DL28svF1OvAw7NS_xkIgnQ,10
19
19
  xtn_tools_pro/utils/retry.py,sha256=0wjHsR5DBBKpv4naMfxiky8kprrZes4WURIfFQ4H708,1657
20
20
  xtn_tools_pro/utils/sql.py,sha256=EAKzbkZP7Q09j15Gm6o0_uq0qgQmcCQT6EAawbpp4v0,6263
21
21
  xtn_tools_pro/utils/time_utils.py,sha256=TUtzG61PeVYXhaQd6pBrXAdlz7tBispNIRQRcGhE2No,4859
22
- xtn_tools_pro-1.0.0.2.7.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- xtn_tools_pro-1.0.0.2.7.dist-info/METADATA,sha256=6oMhQBQO3wynl_-fvRDrKegTZyQqGiWe4y0UK-h-dik,455
24
- xtn_tools_pro-1.0.0.2.7.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
- xtn_tools_pro-1.0.0.2.7.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
- xtn_tools_pro-1.0.0.2.7.dist-info/RECORD,,
22
+ xtn_tools_pro-1.0.0.2.9.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
+ xtn_tools_pro-1.0.0.2.9.dist-info/METADATA,sha256=X-2RKycjnQIprqWWv9A4TIZzRi7cYBEYB-e5SckIh4U,455
24
+ xtn_tools_pro-1.0.0.2.9.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
+ xtn_tools_pro-1.0.0.2.9.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
+ xtn_tools_pro-1.0.0.2.9.dist-info/RECORD,,