Free tools Windows power users keep installed
One-click scans. No signup required.
Use Python’s built-in socket module to connect to a server, send bytes, and receive bytes. The server must already be listening on the host and port you specify. A successful TCP connection establishes a reliable byte stream; it does not define what the application should send or how to tell when a reply is complete.
A minimal TCP client
This blocking example uses socket.create_connection(), sends a byte string, then reads up to 1,024 bytes:
As an Amazon Associate I earn from qualifying purchases.
import socket
HOST = "example.com"
PORT = 50007
with socket.create_connection((HOST, PORT), timeout=5) as sock:
sock.sendall(b"Hello")
response = sock.recv(1024)
print(response)
Replace the example host, port, and message with values for a server that is running and expects those bytes. The example prints the reply as bytes, such as b'Hello'; decode it only if the server’s protocol specifies a text encoding.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsThe with block closes the socket when the block ends, including if an error occurs. The timeout applies to socket operations; it does not guarantee every operating system will report a connection timeout on the same schedule.
#1 Best Overall
Connection setup is separate from the protocol
TCP provides a connected stream of bytes, not application-level message boundaries. sendall() sends the supplied bytes, while recv(1024) returns up to 1,024 bytes currently available. It may return fewer, and one call is not a general guarantee that a whole response has arrived.
To build a complete exchange, follow the server’s protocol: it must specify the message format, how the receiver detects the end of a message (for example, a length field or delimiter), and what response to expect. The short send-and-receive pattern is suitable only when the peer’s protocol makes that exchange clear.
Rank #2
Choose how to create the connection
| Approach | Example | When it fits |
|---|---|---|
| Convenience function | socket.create_connection((HOST, PORT), timeout=5) |
Simplifies connecting to a host and port. For a hostname, Python tries available IPv4 and IPv6 addresses in turn until one connects. |
| Explicit socket | socket.socket(socket.AF_INET, socket.SOCK_STREAM) followed by connect((HOST, PORT)) |
Makes the socket’s address family and stream type explicit. This form is useful when you want to show or control socket construction directly. |
Here is the explicit IPv4 form:
import socket
HOST = "example.com"
PORT = 50007
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(5)
sock.connect((HOST, PORT))
sock.sendall(b"Hello")
response = sock.recv(1024)
print(response)
The two approaches are API choices, not a performance ranking. The explicit example requests IPv4 with AF_INET; create_connection() can try both IPv4 and IPv6 for hostnames.
Recommended Free Tools
Client versus server socket
A client initiates a connection with connect(). It does not use the server’s listening sequence of bind(), listen(), and accept(). A server listens at its destination host and port, accepts an incoming connection, and exchanges data using the accepted socket; the client connects and exchanges data using its own socket.
When a connection fails
- Connection refused: Check that the host and port are correct and that a server is listening and reachable there.
- Timeout: Check the destination and network availability. Python’s socket timeout is useful for limiting waits, but the operating system’s network stack can also determine timeout behavior.
- Hostname or address-family issue: With
create_connection(), Python resolves a non-numeric hostname and tries available IPv4 and IPv6 addresses. If you construct anAF_INETsocket yourself, you have explicitly chosen IPv4.
Connection failures are reported as exceptions by the socket APIs. Consult Python’s socket module documentation for API details, and the Socket Programming HOWTO for an introduction to blocking sockets and client-server communication.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




