Python > Working with External Resources > Networking > Sockets Programming (`socket` module)
Simple Socket Server and Client in Python
This snippet demonstrates the creation of a basic socket server and client for simple communication over a network. It showcases the fundamental steps involved in establishing a connection, sending data, and receiving responses using Python's socket
module.
Server-Side Code
This code sets up a simple TCP server. It binds to a specific IP address and port, listens for incoming connections, accepts a connection, and then receives and echoes data back to the client until the client closes the connection. socket.AF_INET
specifies the IPv4 address family, and socket.SOCK_STREAM
specifies the TCP protocol. The with
statement ensures that the socket is properly closed when the block is exited, even if errors occur.
import socket
# Server configuration
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print(f"Server listening on {HOST}:{PORT}")
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
print(f"Received: {data.decode()}")
conn.sendall(data)
Client-Side Code
This code sets up a simple TCP client. It connects to the server at the specified IP address and port, sends a message ('Hello, server!'), and then waits for a response from the server. The sendall
method ensures that all data is sent. The recv
method receives up to 1024 bytes of data. The received data is then decoded and printed to the console. The with
statement ensures that the socket is properly closed when the block is exited.
import socket
# Client configuration
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, server!')
data = s.recv(1024)
print(f"Received: {data.decode()}")
Concepts Behind the Snippet
This snippet illustrates the client-server model, a fundamental concept in networking. The server listens for connections on a specific port, while the client initiates a connection to the server. Data is exchanged between the client and server over the established connection using sockets. Sockets provide an interface for applications to send and receive data over a network using various protocols, such as TCP (SOCK_STREAM
) and UDP (SOCK_DGRAM
).
Real-Life Use Case
Sockets are the foundation for many network applications, including web servers, chat applications, and game servers. For example, a web server uses sockets to listen for incoming HTTP requests from clients (web browsers) and send back the corresponding HTML content. Chat applications use sockets to facilitate real-time communication between users.
Best Practices
ssl
module in Python provides support for secure sockets.with
statement provides a convenient way to ensure that sockets are closed.select
and asyncio
modules provide support for asynchronous I/O.
Interview Tip
Be prepared to explain the difference between TCP and UDP sockets. TCP provides reliable, ordered, and error-checked delivery of data, while UDP provides a connectionless, unreliable datagram service. TCP is suitable for applications that require guaranteed delivery, such as web browsing and email, while UDP is suitable for applications that can tolerate some data loss, such as streaming video and online games.
When to use them
Use socket programming when you need direct control over network communication, such as when implementing custom protocols or interacting with low-level network devices. Libraries like requests
and frameworks like Flask abstract away many socket details for common HTTP-based tasks. Sockets are essential when you need fine-grained control.
Memory Footprint
The memory footprint of socket operations is relatively low. The primary memory usage comes from the buffers used to send and receive data. The size of these buffers can be configured to optimize memory usage. For high-volume applications, consider using techniques such as memory pooling to reduce the overhead of buffer allocation.
Alternatives
requests
(for HTTP requests) and Twisted
(for asynchronous networking) provide more convenient and easier-to-use interfaces.
Pros
Cons
FAQ
-
What is the difference between TCP and UDP?
TCP (Transmission Control Protocol) is a connection-oriented protocol that provides reliable, ordered, and error-checked delivery of data. UDP (User Datagram Protocol) is a connectionless protocol that provides an unreliable datagram service. TCP is suitable for applications that require guaranteed delivery, while UDP is suitable for applications that can tolerate some data loss. -
What is the purpose of the
bind
method?
Thebind
method associates the socket with a specific IP address and port. This allows the server to listen for incoming connections on that address and port. -
What is the purpose of the
listen
method?
Thelisten
method puts the socket into a listening state, waiting for incoming connections. The argument to thelisten
method specifies the maximum number of queued connections. -
What is the purpose of the
accept
method?
Theaccept
method accepts an incoming connection and returns a new socket object representing the connection. Theaccept
method blocks until a connection is available. -
Why is
sendall
preferred oversend
?
sendall
attempts to send the entire message.send
may only send part of the message, requiring you to loop and resend the remainder.sendall
handles this looping internally, making it more reliable.