Просмотр исходного кода

add log and multiprocessing thread

xubo 1 год назад
Родитель
Сommit
fb63048f69
2 измененных файлов с 50 добавлено и 11 удалено
  1. 43 11
      livepocket/302_poc_2.py
  2. 7 0
      tools/utils.py

+ 43 - 11
livepocket/302_poc_2.py

@@ -1,16 +1,19 @@
 import json
 import multiprocessing
 import re
+import threading
 import time
+from datetime import datetime
 from urllib import parse
 
 import requests
 
-from tools.utils import get_random_browser, get_proxy_ip
+from tools.utils import get_random_browser, get_proxy_ip, create_file_if_not_exists
 
 
-def poc_2(account, passwd, target_url):
+def poc_2(account, passwd, target_url, log_name):
     print('account: %s, passwd: %s' % (account, passwd))
+    create_file_if_not_exists(log_name, f'------{account}, {passwd}------')
     user_agent = get_random_browser()
     single_proxy = get_proxy_ip()
     # single_proxy = 'rrehqcjf:33f90umk8x32@185.199.228.220:7300'
@@ -20,6 +23,7 @@ def poc_2(account, passwd, target_url):
     }
 
     print('proxies:', proxies)
+    create_file_if_not_exists(log_name, f'account: {account}, passwd: {passwd}, proxies:{proxies}')
 
     # session
     """
@@ -122,7 +126,8 @@ def poc_2(account, passwd, target_url):
 
     print('form_data:', form_data)
 
-    response = result.post(net_url, headers=header, cookies=cookies, data=form_data, allow_redirects=False, proxies=proxies)
+    response = result.post(net_url, headers=header, cookies=cookies, data=form_data, allow_redirects=False,
+                           proxies=proxies)
     print('response.headers: ', response.headers)
     redirect_url1 = response.headers['Location']
     print('redirect_url1: ', redirect_url1)
@@ -171,22 +176,49 @@ def poc_2(account, passwd, target_url):
         'reserve_ticket[0][amount]': 1,
         'payment_type': 'credit'
     }
-    drawing_response = result.post(drawing_url, headers=drawing_headers, cookies=drawing_cookies, data=drawing_form_data, allow_redirects=False, proxies=proxies)
+    drawing_response = result.post(drawing_url, headers=drawing_headers, cookies=drawing_cookies,
+                                   data=drawing_form_data, allow_redirects=False, proxies=proxies)
     print('drawing_response.text: ', drawing_response.text)
     drawing_data = json.loads(drawing_response.text)
     if drawing_data['success']:
         order_id = drawing_data['result']['order_id']
         onetime_token_name = drawing_data['result']['onetime_token_name']
         onetime_token_value = drawing_data['result']['onetime_token_value']
-        print('account, order_id, onetime_token_name, onetime_token_value', account, order_id, onetime_token_name, onetime_token_value)
+        print('account, order_id, onetime_token_name, onetime_token_value', account, order_id, onetime_token_name,
+              onetime_token_value)
+
+        create_file_if_not_exists(log_name, f'account:{account}, order_id:{order_id}, onetime_token_name:{onetime_token_name}, onetime_token_value:{onetime_token_value}')
+
+
+def process_account(account, log_name):
+    try:
+        poc_2(account.strip(), 'panyue666', 'https://t.livepocket.jp/e/cx591', log_name)
+    except Exception as e:
+        print(f'error: {e}, account:{account}')
+
+
+def process_running():
+    log_name = 'result_' + datetime.now().strftime('%Y%m%d%H%M')
+    with open('account.txt', 'r') as accounts:
+        account_list = accounts.readlines()
+
+    threads = []
+    for account in account_list:
+        thread = threading.Thread(target=process_account, args=(account, log_name))
+        threads.append(thread)
+        thread.start()
+
+    for thread in threads:
+        thread.join()
 
 
 if __name__ == '__main__':
     # 新增下面一行代码即可打包多进程
     multiprocessing.freeze_support()
-    with open('account.txt', 'r') as accounts:
-        for account in accounts:
-            try:
-                poc_2(account.strip(), 'panyue666', 'https://t.livepocket.jp/e/cx591')
-            except Exception as e:
-                print(f'error: {e}, account:{account}')
+    # with open('account.txt', 'r') as accounts:
+    #     for account in accounts:
+    #         try:
+    #             poc_2(account.strip(), 'panyue666', 'https://t.livepocket.jp/e/cx591')
+    #         except Exception as e:
+    #             print(f'error: {e}, account:{account}')
+    process_running()

+ 7 - 0
tools/utils.py

@@ -169,6 +169,13 @@ class FileWriter:
             file.write(content)
 
 
+def create_file_if_not_exists(file_path, content):
+    if not os.path.exists(file_path):
+        with open(file_path, 'w') as f:
+            print(content, file=f)
+            print(f'File {file_path} created successfully!')
+
+
 if __name__ == "__main__":
     # 代理池
     print(get_proxy_ip())