Skip to content

Instantly share code, notes, and snippets.

@robbmanes
Created March 17, 2021 16:24
Show Gist options
  • Save robbmanes/4e96fce56b8752245f52b5eb99b6b9ce to your computer and use it in GitHub Desktop.
Save robbmanes/4e96fce56b8752245f52b5eb99b6b9ce to your computer and use it in GitHub Desktop.
Script that sets up a server to accept connections and a client to flood the server with connections.
import logging
import socket
import sys
import threading
import time
CLIENT_CONN_ADDR="127.0.0.1"
CLIENT_NUM_CONNS=10
SERVER_PORT=8888
SERVER_SYN_BACKLOG=128
SERVER_WAIT_BEFORE_CLOSE=True
SERVER_WAIT_BEFORE_CLOSE_SECS=3
HELP_MESSAGE="Please specify either \"server\" or \"client\" as an argument."
def handle_client_conn(conn, addr):
try:
logging.info("Handling new client connection from %s:%d" % (addr[0], addr[1]))
if SERVER_WAIT_BEFORE_CLOSE:
time.sleep(SERVER_WAIT_BEFORE_CLOSE_SECS)
logging.info("Closing connection to %s:%d" % (addr[0], addr[1]))
conn.close()
except Exception as e:
logging.error("Error handling connection %s: %s" % (addr, e))
def conn_to_server():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
logging.info("Connecting to server %s..." % (CLIENT_CONN_ADDR))
start_time = time.time()
sock.connect((CLIENT_CONN_ADDR, SERVER_PORT))
end_time = time.time()
conn_time = end_time - start_time
logging.info("New connection took %.4f seconds" % conn_time)
logging.info("Succesful connection to server.")
sock.close()
except Exception as e:
logging.error("Error connection to server: %s" % e)
def main():
if len(sys.argv) != 2:
print(HELP_MESSAGE)
return
if sys.argv[1] == "client":
server = False
elif sys.argv[1] == "server":
server = True
else:
print(HELP_MESSAGE)
return
logging.basicConfig(level=logging.DEBUG)
threads = []
if server:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("0.0.0.0", SERVER_PORT))
sock.listen(SERVER_SYN_BACKLOG)
logging.info("Server now listening on 0.0.0.0:%d..." % SERVER_PORT)
while True:
try:
conn, addr = sock.accept()
thread = threading.Thread(target=handle_client_conn, args=(conn, addr))
threads.append(thread)
thread.start()
except Exception as e:
logging.error("Failed to handle connection %s with error %s" % (addr, e))
except KeyboardInterrupt:
logging.info("Got Ctrl-C, killing threads")
for t in threads:
t.kill_received = True
sock.close()
return
else:
num_of_conns = 0
try:
while True:
try:
if num_of_conns >= CLIENT_NUM_CONNS:
logging.info("Hit maximum number of client connections outgoing, sleeping.")
break
else:
thread = threading.Thread(target=conn_to_server)
threads.append(thread)
thread.start()
num_of_conns = num_of_conns + 1
except Exception as e:
logging.error("Failed to connect to server: %s" % e)
while True:
time.sleep(10)
except KeyboardInterrupt:
logging.info("Got Ctrl-C, killing threads")
for t in threads:
t.kill_received = True
return
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment