How to change iterator of a function with multiprocessing by condition from inside of a process?

Viewed 34

I want to use next one iterator if my condition inside of a Process is triggered:

from multiprocessing import Process
import requests
import os


def worker(url, data):
    try:
        response = requests.post(url, data=data, timeout=15)
        if r"Match" in response.text:
            print('Match!')
        if r"Wrong word" in response.text:
            print('Wrong word!')
            # CHANGE URL HERE
    except Exception as e:
        print(str(e))


def init():
    # http://localhost:3000/set_phrase0\n http://localhost:3000/set_phrase1\n http://localhost:3000/set_phrase2\n...
    urls = open('urls.txt').read().splitlines()
    for url in urls:
        # if 'Wrong word' in response.text -> change url here and start with first file again
        for file in os.listdir('wordlists'):  # directory with generated words
            words = open(f'wordlists/{file}').read().splitlines()
            for word in words:
                data = {'word': word}
                Process(target=worker, args=(url, data)).start()


if __name__ == '__main__':
    init()
0 Answers
Related