Socket Library for Python
Socket
A socket is an endpoint of a bidirectional inter-process communication channel. Sockets are used to provide communication between processes on the same machine, or between processes on different machines over a network.
In other words, it is an endpoint for communication between two machines over a network used to establish, send and receive data as well as close Connection.
Type of sockets
Stream Sockets (
socket.SOCK_STREAM
): These provide a reliable, connection-oriented communication channel. They use protocols like TCP (Transmission Control Protocol) and ensure that data is delivered in the order it was sent.Datagram Sockets (
socket.SOCK_DGRAM
): These are connectionless and provide an unreliable, message-oriented communication channel. They use protocols like UDP (User Datagram Protocol) and do not guarantee delivery order or delivery at all.
Address family
An address family is a set of network addresses that share a common format. The most common address families are AF_INET and AF_INET6, which are used for IPv4 and IPv6 addresses, respectively.
Pv4 (
socket.AF
_INET
): This is used for IPv4 addresses and provides a 32-bit address space.IPv6 (
socket.AF
_INET6
): This is used for IPv6 addresses which have a much larger address space.
For creating a Socket
import socket
# Create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
For listing and accepting connections
For a server, it needs to start listening for incoming connections. When a client tries to connect, the server uses accept()
to establish a connection.
server_socket.listen(5)
client_socket, client_address = server_socket.accept()
Connecting to a server
For a client, it needs to connect to a server using the server's address and port.
client_socket.connect(server_address)
Sending and receiving data
Once a connection is established, data can be sent and received using send()
and recv()
.
client_socket.send(data.encode())
received_data = client_socket.recv(1024)
Closing a socket
After communication is complete, the sockets should be closed.
client_socket.close()
Client-server connection using socket library
Client-server connection using Python's socket
library. This example demonstrates basic TCP communication between a server and a client.
Server code
import socket
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server address and port
server_address = ('localhost', 12345)
# Bind the socket to the server address and port
server_socket.bind(server_address)
# Listen for incoming connections (maximum of 1 connection in the queue)
server_socket.listen(1)
print(f"Server is listening on {server_address[0]}:{server_address[1]}")
# Accept incoming connection
client_socket, client_address = server_socket.accept()
# Receive and print data from the client
data = client_socket.recv(1024)
print(f"Received: {data.decode()}")
# Send a response back to the client
message = "Hello, client! This is the server."
client_socket.sendall(message.encode())
# Close the sockets
client_socket.close()
server_socket.close()
Client code
import socket
# Create a TCP/IP socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Define the server address and port
server_address = ('localhost', 12345)
# Connect the socket to the server address and port
client_socket.connect(server_address)
# Send data to the server
message = "Hello, server! This is the client."
client_socket.sendall(message.encode())
# Receive and print data from the server
data = client_socket.recv(1024)
print(f"Received: {data.decode()}")
# Close the socket
client_socket.close()
Then run server code "python server.py" and then client code "python client.py"
After this, you will receive the output.
Here's how it works:
The server creates a TCP/IP socket and binds it to a specific address and port.
It listens for incoming connections using
server_socket.listen(1)
.When a client attempts to connect,
server_socket.accept()
accepts the incoming connection and returns a new socket (client_socket
) and the address of the client.The server receives data from the client using
client_socket.recv(1024)
.It then sends a response back to the client using
client_socket.sendall(message.encode())
.Both the client and server close their respective sockets after the communication.
Make sure to run the server code first, followed by the client code. You can adjust the address and port as needed for your specific use case.