The socket module in Python provides a low-level interface to network sockets, which are the fundamental building blocks of network communication. Sockets allow processes on different devices to communicate over a network. Here are some key aspects of the socket module:
-
Socket Types:
- The
socketmodule supports various socket types, including:socket.SOCK_STREAM: Provides a reliable, stream-oriented connection (TCP).socket.SOCK_DGRAM: Supports connectionless, unreliable datagrams (UDP).socket.SOCK_RAW: Provides raw socket access to transport layer protocols.
- The socket type is specified when creating a socket.
- The
-
Socket Families:
- The
socketmodule supports different address families, such assocket.AF_INETfor IPv4 andsocket.AF_INET6for IPv6. - The address family is specified when creating a socket.
- The
-
Socket Creation:
- To create a socket, you use the
socket()function, specifying the socket family and type. - Example:
import socket # Create a TCP socket tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- To create a socket, you use the
-
Socket Binding:
- To bind a socket to a specific address and port, you use the
bind()method. - Example:
# Bind the TCP socket to a specific address and port tcp_socket.bind(('localhost', 8080))
- To bind a socket to a specific address and port, you use the
-
Socket Listening and Accepting Connections:
- For server sockets (TCP), you use the
listen()method to start listening for incoming connections, andaccept()to accept a connection. - Example:
# Listen for incoming connections tcp_socket.listen() # Accept a connection client_socket, client_address = tcp_socket.accept()
- For server sockets (TCP), you use the
-
Socket Connection (Client):
- For client sockets (TCP), you use the
connect()method to establish a connection to a server. - Example:
# Connect to a server tcp_socket.connect(('localhost', 8080))
- For client sockets (TCP), you use the
-
Socket Sending and Receiving Data:
- For both client and server sockets, you use the
send()andrecv()methods to send and receive data. - Example:
# Send data tcp_socket.send(b'Hello, Server!') # Receive data data = tcp_socket.recv(1024)
- For both client and server sockets, you use the
-
Socket Closing:
- To close a socket, you use the
close()method. - Example:
# Close the socket tcp_socket.close()
- To close a socket, you use the
-
Error Handling:
- The
socketmodule raises various exceptions (e.g.,socket.error) that you can catch for error handling.
- The
Here’s a simple example of a TCP server and client using the socket module:
The server and client socket methods
Server Socket Methods
Python
import socket
# Create a TCP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to a specific address and port
server_socket.bind(('localhost', 8080))
# Listen for incoming connections
server_socket.listen()
print("Server listening on port 8080")
# Accept a connection
client_socket, client_address = server_socket.accept()
print(f"Connection established with {client_address}")
# Receive data from the client
data = client_socket.recv(1024)
print(f"Received data: {data.decode('utf-8')}")
# Send a response to the client
response = "Hello, Client!"
client_socket.send(response.encode('utf-8'))
# Close the sockets
client_socket.close()
server_socket.close()Client Socket Methods
Python
import socket
# Create a TCP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the server
client_socket.connect(('localhost', 8080))
# Send data to the server
message = "Hello, Server!"
client_socket.send(message.encode('utf-8'))
# Receive a response from the server
response = client_socket.recv(1024)
print(f"Received response: {response.decode('utf-8')}")
# Close the socket
client_socket.close()