我如何更好地处理我的请求垃圾邮件发送者的多线程

2024-04-26 05:15:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个小python脚本,用于使用请求向URL发送垃圾邮件请求,它使用代理,因此IP是随机的,我有一个名为https.txt的文本文件夹,其中有2000多个代理

我试图对程序进行多线程处理,但我觉得我所做的并不完全适合我所做的。如果我能得到任何关于如何改进的建议,我将不胜感激

from colorama import Fore, Back, Style, init
import requests
import threading

init()

# constant vars
link = "" # URL goes here
agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
referer = ""
proxies = open("https.txt", "r")

# dynamic vars
threadLock = threading.Lock()
threads = []
num = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter, proxy):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.proxy = proxy
    def run(self):
        # print("Starting " + self.name)
        spam(self.name, proxy)

# Each thread runs this function once
def spam(threadName, proxy):
    try:
        headers = {
            "user-agent": agent,
            "referer": referer
        }
        req = requests.get(url=link, headers=headers, proxies=proxy, timeout=100)
        status = req.status_code
        req.close()
        if status == 200:
            print(Fore.CYAN + threadName + ": Working request with proxy: " + Fore.YELLOW + x.strip())
        else:
            print(Fore.GREEN + threadName + ": Connection Code Status Error:", status)
    except IOError :
        print(Fore.RED + threadName + ": Connection error - Bad Proxy")


for x in proxies:
    thread = str(num)
    num = num + 1
    proxy = {
        "https": x.strip()
    }
    thread = myThread(thread, "Thread-" + thread, num, proxy)
    thread.start()

# Wait for all threads to complete
for t in threads:
    t.join()

Tags: namehttpsimportselfinitstatusthreadnum