简体   繁体   English

如何在我的 Python 代码上添加线程?

[英]How can I add threading on my Python code?

Below is my try to create a username availability checker with proxies, so far it works as intended the only thing is that its slow, i tried to implement threads but no different as im not sure if im doing it right or not.下面是我尝试使用代理创建用户名可用性检查器,到目前为止它按预期工作,唯一的问题是它很慢,我尝试实现线程但没有什么不同,因为我不确定我是否做对了。 used concurrent.futures and threading libraries.使用 concurrent.futures 和线程库。

Is there a better way to code this kind of programs or are there any other suggestions?有没有更好的方法来编写这种程序,或者还有其他建议吗? Thanks in advance提前致谢

import requests
import json
import ctypes
import colorama
from colorama import Fore
from datetime import datetime
import os

os.system("cls")
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
colorama.init()
url = "https://link"

def grab_proxies():
    proxylist = []
    prx = open('proxy.txt','r')
    prx = prx.readlines()
    for proxy in prx:
        proxy = proxy.rstrip("\n")
        proxylist.append(proxy)
    return proxylist
prlist = grab_proxies()

def grab_usernames():
    userlist = []
    users = open('userlist.txt','r')
    users = users.readlines()
    for user in users:
        user = user.rstrip("\n")
        userlist.append(user)
    return userlist
ulist = grab_usernames()


found = 0
pc = 0
uc = 0
for i in range(0,len(prlist)):
    ctypes.windll.kernel32.SetConsoleTitleW(f"[# Checker] | Counter: %s - Found: %s - Current Proxy: %s - Started at: %s" % (i, found, prlist[pc], current_time))
    try:
        req = requests.post(url,headers=headers, data = {"requested_username": ulist[uc], "xsrf_token": "F0kpyvjJgeBtsOk5Gl6Jvg"},proxies={'http' : prlist[pc],'https': prlist[pc]}, timeout=2)
        response = req.json()
        #print(response,req.status_code)
        #print(response)
        #print(type(response))
        if(response['reference']['status_code'] == 'TAKEN'):
            #rd = response['errors']['username'][0]['code']
            print(f'{Fore.LIGHTBLACK_EX}[{Fore.LIGHTRED_EX}Taken{Fore.LIGHTBLACK_EX}]{Fore.LIGHTCYAN_EX} {ulist[uc]}')
            #print(ulist[uc]+" Taken")
            uc+=1
        elif(response['reference']['status_code'] == 'OK'):
            print(f'{Fore.LIGHTBLACK_EX}[{Fore.LIGHTGREEN_EX}Available{Fore.LIGHTBLACK_EX}]{Fore.LIGHTCYAN_EX} {ulist[uc]}')
                #print(ulist[uc]+" Available")
            f = open("found.txt","a")
            f.write(ulist[uc]+"\n")
            f.close()
            found+=1
            uc+=1
        elif(response['reference']['status_code'] == 'INVALID_BEGIN'):
            print(f'{Fore.LIGHTBLACK_EX}[{Fore.LIGHTRED_EX}Invalid Username{Fore.LIGHTBLACK_EX}]{Fore.LIGHTCYAN_EX} {ulist[uc]}')
            uc+=1
        elif(response['reference']['status_code'] == 'DELETED'):
            print(f'{Fore.LIGHTBLACK_EX}[{Fore.LIGHTRED_EX}Deleted{Fore.LIGHTBLACK_EX}]{Fore.LIGHTCYAN_EX} {ulist[uc]}')
            uc+=1
        else:
            print(response)
    except:
        #print(prlist[pc]+ " Going to next proxy")
        pc+=1
        pass
#break

x = input("Finished!.. press enter to exit")

You could use https://github.com/encode/requests-async to do your requests in an async way您可以使用https://github.com/encode/requests-async以异步方式执行您的请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM