Skip to content

Instantly share code, notes, and snippets.

@tsaarni
Last active September 21, 2022 12:30
Show Gist options
  • Save tsaarni/e075b1e23bf6a110391426a5f7183c6f to your computer and use it in GitHub Desktop.
Save tsaarni/e075b1e23bf6a110391426a5f7183c6f to your computer and use it in GitHub Desktop.
How to simulate TCP connect timeout
#!/bin/env python3
#
# This script can be used as a server when you need to test the handling of
# TCP connection establishment timeouts of your client.
#
# The protocol can be HTTP, HTTPS or just about anything else, since connection
# will never be established. It will hang in TCP handshake.
#
import socket
# Bind to server port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("",8000))
s.listen(0)
# Connect one client to fill in the listen queue
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(("127.0.0.1",8000))
# At this point, testing the client connect timeout handling can take place.
# Any attempt to establish connection from external clients will not complete.
# TCP tree-way handshake cannot be completed because TCP implementation at the
# server will not send SYN+ACK back until there is room in the listen queue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment