Python Curriculum
On this page
- Socket Module
- What is a Network Socket?
- How Sockets Establish Connections
- Basic Socket Properties Example
- Expected Output
- Real-World DevOps Scenario: Server Port Checker
- Expected Output
- Common Mistakes
- 1. Forgetting to encode strings before sending
- 2. Forgetting to set timeouts
- 3. Leaking sockets by not closing them
- Summary
Socket Module
In DevOps, everything runs on the network. When you trigger a deployment, query a database, curl a web server, or pull a container image, computers are talking to each other. The low-level interface that allows operating systems to open connections across a network is called a socket.
Python provides a built-in module named socket. It lets you write code that talks directly to network interfaces, allowing you to build port scanners, create custom servers, check server availability, and resolve hostnames.
In this guide, we will explore the core concepts of the socket module, how network connections are initiated, and how to query socket parameters.
What is a Network Socket?
A socket is one endpoint of a two-way communication link between two programs running on a network. A socket is bound to a combination of an IP Address (where the machine is located) and a Port Number (which specific program should receive the traffic).
When configuring sockets, you must define their family (how they find hosts) and their type (how they transport packets).
Let us compare the most common families and types:
How Sockets Establish Connections
The connection process between a server socket and a client socket follows a strict sequence:
Basic Socket Properties Example
Let us write a simple Python script to create a TCP socket and inspect its default configurations.
Expected Output
Real-World DevOps Scenario: Server Port Checker
In automated deployments, you often want to wait until a service (like a database or web server) is fully started and listening on its port before your script tries to configure it. Here is a script that checks if a port is open.
Expected Output
Common Mistakes
Here are some typical gotchas when using the socket module:
1. Forgetting to encode strings before sending
Sockets send raw bytes, not Unicode strings. If you try to run s.send("hello"), your script will crash with a TypeError. You must encode your strings to bytes using .encode('utf-8') before transmission, and decode received bytes using .decode('utf-8').
2. Forgetting to set timeouts
By default, socket connections have no timeout (timeout=None). If you try to connect to a server that is blocked by a firewall, your script can hang indefinitely, blocking your CI/CD runner. Always set a timeout using s.settimeout(2.0).
3. Leaking sockets by not closing them
Each socket connection uses a file descriptor on the operating system. If you open sockets inside loops and forget to call s.close(), you will hit the OS limits, raising OSError: [Errno 24] Too many open files. Use with statements to ensure sockets close automatically.
Summary
- Sockets are the low-level API endpoints that allow processes to communicate across networks.
- Sockets require an IP address (location) and a port number (destination application).
- Use
socket.AF_INETfor standard IPv4 andsocket.SOCK_STREAMfor reliable TCP transmission. - Close sockets using
s.close()or by running them insidewithstatement blocks. - Use
s.settimeout(seconds)to prevent automation scripts from hanging indefinitely on dead connections. - Check connection status without raising exceptions by calling
s.connect_ex((ip, port)). - Convert string data to bytes using
.encode()before sending data over sockets. - Convert raw bytes to readable strings using
.decode()when receiving socket data. - Catch hostname lookup failures by trapping the
socket.gaierrorexception class.