xtn-tools-pro 1.0.0.2.6__py3-none-any.whl → 1.0.0.2.8__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,36 +8,201 @@
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_file=""):
18
- if not ini_file:
19
- ini_current_dir = os.getcwd()
20
- ini_file = os.path.join(ini_current_dir, "st.ini")
21
- if not check_file_exists(ini_file):
22
- raise Exception("st.ini 配置文件不存在,请在当前文件夹中新建该配置文件")
23
-
21
+ def __init__(self, ini_dict, logger, go_task_function=None):
24
22
  # 读取配置信息
25
- config = configparser.ConfigParser()
26
- config.read(ini_file)
27
- server_host = config.get('server', 'host')
28
- server_port = config.get('server', 'port')
29
- server_task = config.get('server', 'task')
30
- server_auto = config.get('server', 'auto')
31
-
32
- print(server_host, server_port, server_task)
33
- if server_port:
34
- task_host = f"http://{server_host}:{server_port}"
23
+ host = ini_dict.get('host', '')
24
+ port = ini_dict.get('port', 0)
25
+ task = ini_dict.get('task', '')
26
+ auto = ini_dict.get('auto', '')
27
+ processes_num = ini_dict.get('processes_num', 0)
28
+ thread_num = ini_dict.get('thread_num', 0)
29
+ restart_time = ini_dict.get('restart_time', 0)
30
+
31
+ self.__ini_info = {
32
+ "host": host,
33
+ "port": port,
34
+ "task": task,
35
+ "auto": auto,
36
+ "processes_num": processes_num,
37
+ "thread_num": thread_num,
38
+ "restart_time": restart_time,
39
+ }
40
+
41
+ for server_k, server_v in self.__ini_info.items():
42
+ if not server_v and server_k not in ["port", "processes_num", "thread_num", "restart_time"]:
43
+ raise Exception(f"ini_dict 配置 {server_k} 不存在")
44
+
45
+ if port:
46
+ task_host = f"http://{host}:{port}"
35
47
  else:
36
- task_host = f"http://{server_host}"
48
+ task_host = f"http://{host}"
37
49
 
38
50
  download_url = task_host + "/filter_server/phone/get"
39
51
  upload_url = task_host + "/filter_server/phone/update"
52
+ external_ip = self.__get_external_ip()
53
+
54
+ self.__ini_info["download_url"] = download_url
55
+ self.__ini_info["upload_url"] = upload_url
56
+ self.__ini_info["external_ip"] = external_ip
57
+
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
+ qsize = queue.qsize()
145
+ print("回写任务", qsize)
146
+ time.sleep(1)
147
+
148
+ def _go_task_fun_task(self, download_queue, upload_queue, ini_info, go_task_function, logger):
149
+ """
150
+ 单函数,根据配置启动程序
151
+ :param queue:
152
+ :return:
153
+ """
154
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
155
+ caller_name = caller.function # 获取调用者的函数名
156
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
157
+ if caller_name != "run" or caller_class is None:
158
+ raise Exception("错误调用")
159
+
160
+ processes_num = ini_info["processes_num"]
161
+ thread_num = ini_info["thread_num"]
162
+ restart_time = ini_info["restart_time"]
163
+
164
+ processes_num = 1 if processes_num <= 0 else processes_num
165
+ thread_num = 1 if thread_num <= 0 else thread_num
166
+
167
+ go_task_fun_cnt = 0
168
+ processes_start_list = []
169
+ while True:
170
+ try:
171
+ if not processes_start_list:
172
+ go_task_fun_cnt += 1
173
+ logger.info(
174
+ f"第{go_task_fun_cnt}次,进程数:{processes_num},线程数:{thread_num},等待{restart_time}秒强制下一次")
175
+ for i in range(processes_num):
176
+ p = Process(target=self._run_with_timeout,
177
+ args=(download_queue, upload_queue, thread_num, go_task_function))
178
+ processes_start_list.append(p)
179
+ p.start()
180
+
181
+ if not restart_time:
182
+ # 一直执行 不退出
183
+ continue
184
+
185
+ time.sleep(restart_time)
186
+ # 关闭所有进程
187
+ for p in processes_start_list:
188
+ p.terminate()
189
+ p.join() # 等待进程确实结束
190
+ processes_start_list = []
191
+
192
+ except Exception as e:
193
+ pass
194
+
195
+ def _run_with_timeout(self, download_queue, upload_queue, thread_num, go_task_function):
196
+ caller = inspect.stack()[1] # 获取调用者的调用栈信息
197
+ caller_name = caller.function # 获取调用者的函数名
198
+ caller_class = caller.frame.f_locals.get('self', None) # 获取调用者的类实例
199
+ if caller_name != "run" or caller_class is None:
200
+ raise Exception("错误调用")
201
+
202
+ with concurrent.futures.ThreadPoolExecutor(max_workers=thread_num) as executor:
203
+ # 提交10个函数到线程池中执行
204
+ futures = [executor.submit(go_task_function, download_queue, upload_queue) for _ in range(thread_num)]
40
205
 
41
- print(server_auto)
42
- print(download_url)
43
- print(upload_url)
206
+ # 等待所有线程完成
207
+ for future in concurrent.futures.as_completed(futures):
208
+ future.result()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xtn-tools-pro
3
- Version: 1.0.0.2.6
3
+ Version: 1.0.0.2.8
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=3vAdvr0pPQPuxX6f0sqqszIRbdzqAAeEdiiDqXBGceQ,1593
13
+ xtn_tools_pro/task_pro/go_fun.py,sha256=CccaMMl-Mr_SFO2kUSXH2JKsgNpvE90bxcMDfrqris8,8444
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.6.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
- xtn_tools_pro-1.0.0.2.6.dist-info/METADATA,sha256=8JH_OeHQuVn6PRENDRw5DE_OOTrW4zZ775xkpxzFKY4,455
24
- xtn_tools_pro-1.0.0.2.6.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
- xtn_tools_pro-1.0.0.2.6.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
- xtn_tools_pro-1.0.0.2.6.dist-info/RECORD,,
22
+ xtn_tools_pro-1.0.0.2.8.dist-info/LICENSE,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
+ xtn_tools_pro-1.0.0.2.8.dist-info/METADATA,sha256=vPJnzEL5yxvzlRNSY18RO7fUZTtk-FQmTaHZ8MVEzKA,455
24
+ xtn_tools_pro-1.0.0.2.8.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
25
+ xtn_tools_pro-1.0.0.2.8.dist-info/top_level.txt,sha256=jyB3FLDEr8zE1U7wHczTgIbvUpALhR-ULF7RVEO7O2U,14
26
+ xtn_tools_pro-1.0.0.2.8.dist-info/RECORD,,