Socket in Python

Socket in Python

ยท

5 min read

Sockets in Python allows different programs on a computer or across the internet to talk to each other. Socket serves as a communication endpoint that enables data exchange between two programs (or processes) running on different devices connected to a network.

Python socket module is a versatile and robust library that serves as a gateway for Python programs to interact with the Berkeley sockets API.

๐Ÿ
Berkeley sockets are a set of APIs (Application Programming Interfaces) used for network communication. Provides a consistent interface for creating, configuring, and managing network sockets, which are endpoints for communication between two devices over a network.

Communication protocols

Before creating a socket, it is essential to understand the differences between TCP and UDP , as these are the two primary communication protocols used over the internet.

Transmission Control Protocol (TCP):

  1. Reliable and Connection-Oriented: TCP ensures reliable, ordered, and error-checked delivery of data between applications. It establishes a connection before data transfer begins and guarantees that data arrives at its destination intact and in the correct order.

  2. Usage Scenarios: TCP is commonly used for applications that require high reliability and integrity of data transmission, such as web browsing, email, file transfer (FTP).

User Datagram Protocol (UDP):

  1. Unreliable and Connectionless: UDP provides a simple, lightweight, and connectionless communication model. It does not guarantee delivery of data packets and does not establish a connection before transmitting data.

  2. Low Overhead and Minimal Latency: UDP has lower overhead and minimal latency compared to TCP, making it suitable for applications where real-time communication and speed are prioritized over reliability, such as streaming media, online voice/video calling, DNS (Domain Name System), and online gaming.

When creating a socket, you'll need to choose between TCP and UDP based on your application's requirements.

Important Concepts

After understanding the differences between TCP and UDP, and before creating a socket, it's important to get familiarize with the following concepts:

  1. IP Addressing: An Internet Protocol address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. It serves as an identifier for the device within the network. There are two main types of IP addresses: IPv4 and IPv6.
    IPv4 addresses are typically written in dotted-decimal notation e.g., 192.168.0.1, while IPv6 addresses are longer and written in hexadecimal notation (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334

  2. Ports: Ports are numerical identifiers used to differentiate between different communication channels on a single network device. They enable multiple services or applications to operate simultaneously on the same device.

    Port numbers range from 0 to 65535. Ports 0 to 1023 are reserved for well-known services e.g., HTTP uses port 80, HTTPS uses port 443.

  3. Server and Client Architecture: In the client-server architecture, Servers provide resources or services such as web pages, files to clients, while clients request and consume these resources.

    • Server Role: Servers are powerful computers or software applications that listen for incoming connections from clients and respond to their requests. They provide services or resources based on client requests.

    • Client Role: Clients are devices or software applications that initiate connections to servers and request services or resources. They interact with servers to access data, perform actions, or receive responses.

    • Communication: Communication occurs through a request-response mechanism. Clients send requests to servers, and servers process these requests and send back responses containing the requested data or information.

Creating Socket

This code will demonstrate a simple client-server interaction using sockets in Python

Server Side

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 9999))
s.listen(4)
print("waiting for connection")

while True:
    c, addr = s.accept()

    print("connected with", addr)
    c.send("welcome your are connected".encode())
    c.close()
  1. Importing the socket Module: This line will import the socket module, which provides access to socket functionality in Python.

  2. Creating a Socket Object: By using the socket.socket() function a new socket object is created. This function takes two parameters: the address family and the socket type.

    • The address family specifies the network protocol family to be used. When creating a socket with the AF_INET address family, you're indicating that the socket will communicate using IPv4 addresses. AF_INET is the Internet address family for IPv4.
      AF_INET - Internet family for IPv4 only. AF_INET6 - Internet family for IPv6 and IPv4

    • The socket type determines the type of communication.
      SOCK_STREAM represents a TCP socket, while SOCK_DGRAM represents a UDP socket. The protocols that will be used to transport messages in the network.

    tcp_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    udp_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  1. Binding Socket : Binding refers to associating a socket with a specific network address and port number. The bind() method takes a tuple (host, port) as its argument, specifying the hostname or IP address and the port number to bind to. Here localhost represents the local machine, and 9999 is an example port number.

     s.bind(('localhost', 9999))
    
  2. Listen for Connections: This line sets up the server socket s to listen for incoming connections. The argument 4 specifies the maximum number of queued connections. Once the limit is reached, new connection requests will be rejected.

s.listen(4)
print("Waiting for connection")
  1. Accepting Incoming Connections : This creates an loop of continuously accepting the incoming connections.

     while True:
         c, addr = s.accept()
    

    The accept() method blocks until a client connection is established. Upon connection, it returns a new socket c for communication and the address (addr) of the client. It prints the connection details, sends a welcome message back to the client, and closes the connection.

Client Side

import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(('localhost', 9999))

print(c.recv(1024).decode())

The code c.connect(('localhost', 9999)) connects the client socket c to the server running on the same machine (localhost) at port 9999.
It receives a welcome message from the server using the recv() method, which receives up to 1024 bytes of data.
Finally, it decodes the received message from bytes to a string using decode() and prints it to the console.

Overall, this code establishes a connection to a server using a TCP/IP socket, sends the client's name to the server, and receives a welcome message from the server. It demonstrates a basic client-server interaction using sockets in Python.

ย