How to let the user decide how many threads/tasks he wants to run?

Viewed 90

im programming a little Sneaker-Bot in python with selenium and im already finished with the main part. The last two parts i need to programm are, how i can let the user input his Usernames and Passwords for his Accounts in one text file or i heard that a json file would be good too, how i can acces this data with the programm and finally, how i can let the user create threads/tasks with a specific Username and Password for each thread. I hope you can understand my english and maybe there are some guys under you, that can help me with the last part.

1 Answers

For getting the usernames and passwords from a file, you can just use:

f = open(path_to_file)
infolist = f.readlines()
f.close()

This will put a new item in the list for every line in the file, meaning you just have to put the usernames and passwords each on their own line. Be careful because putting passwords in a text file is generally not very secure, so make sure you are aware of the risks.

As for the second part, I would create a list that can be filled with threads using a for loop, the range of which is specified by the user input. Then during the iteration, give the "args" argument certain items of the credentials list that are needed for its task. Example below:

from threading import *

def spawnScraper(username, password):
    print(f'{username} and {password}')
    ## run the instance here

def main():
    #f = open(path_to_file)
    #infolist = f.readlines()
    #f.close()
    # ^ above code to retrieve data from file
    infolist = ['user1', 'pass1', 'user2', 'pass2']  # using for demonstration
    threadnum = int(input('num >>> '))

    threads = []  # create list to store threads

    for threaditem in range(threadnum):
        uname = infolist[threaditem*2]  # get information from infolist
        pword = infolist[threaditem*2+1]
        threads.append(Thread(target=spawnScraper, args=[uname, pword]))  # create thread
        threads[-1].start()  # start thread

main()

Make sure to uncomment the file opening section for actual intergration into your program. If you have any other questions, be sure to comment below.

Related