Python From Beginner to Advanced

0% completed

Previous
Next
Python - Socket Programming

Socket programming is a way to enable communication between two nodes on a network to exchange data. Sockets form the endpoints in a network communication channel, where data sent from one socket is received by another socket at the other end of the communication.

Understanding Sockets

A socket is a software endpoint that establishes bidirectional communication between a server and one or more clients. The socket allows for both listening for incoming data and sending outgoing messages. Sockets operate at the transport layer and can utilize both TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) depending on the requirements of the application.

Python Socket Module

Python provides built-in support for socket programming via the socket module. This module includes the necessary functions to create and manage both server and client sockets, handling tasks such as sending and receiving data.

Sockets Vocabulary

  • Socket: An endpoint of a two-way communication link between two programs running on the network.
  • Bind: Attaching a socket to an IP address and a port number.
  • Listen: For server sockets, this applies to listening for incoming connections.
  • Accept: When a server socket accepts a connection from a client.
  • Connect: Used by a client to establish a connection with the server.
  • Send: Transmit data to a socket.
  • Receive: Receive data from a socket.

Step-by-Step Guide to Socket Programming in Python

In this section, we cover the steps to set up a basic client-server system using Python's socket module. Each step is detailed with appropriate code snippets for both the client and server, showcasing various socket methods.

Client-side Socket programming:

  1. Create a Socket:

    • Create the client socket.
    import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  2. Connect to a Server:

    • Establish a connection to the server using s.connect().
    s.connect(('localhost', 12345))
  3. Receive Data from the Server:

    • Receive data sent by the server using s.recv().
    message = s.recv(1024).decode() print("Received from server: ", message)
  4. Close the Socket:

    • Close the socket once the communication is complete.
    s.close()
Python3
Python3
. . . .

Server-side Socket Programming

  1. Create a Socket:

    • Create the server socket.
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  2. Bind the Socket:

    • Bind the socket to a specific IP and port to listen to incoming requests.
    s.bind(('localhost', 12345))
  3. Listen for Incoming Connections:

    • Start listening for incoming connections with s.listen().
    s.listen(5) print("Server is listening...")
  4. Accept Connections:

    • Accept an incoming client connection with s.accept().
    client_socket, addr = s.accept() print("Got a connection from ", addr)
  5. Send Data to the Client:

    • Send data to the connected client using client_socket.send().
    client_socket.send('Thank you for connecting'.encode())
  6. Close the Client Socket:

    • Close the client socket after sending the data.
    client_socket.close()
Python3
Python3
. . . .

Explanation:

  • Server: Initializes a socket, binds it to a specific port, listens for connections, accepts an incoming connection, sends data, and closes the connection.
  • Client: Creates a socket, connects to the server's IP address and port, receives data from the server, and closes the connection.

This basic example demonstrates the fundamental operations involved in Python socket programming for creating a simple client-server system. By understanding these concepts and following the step-by-step guide, you can develop more complex networked applications in Python.

Socket Methods

Here are the common socket methods presented in table format for clarity:

CategoryFunctionDescription
Server Methodss.bind()Binds address to the socket. The address is a tuple of hostname and port number.
s.listen()Starts the TCP listener, specifying the maximum number of queued connections.
s.accept()Passively accepts a client connection and blocks until the connection arrives.
Client Methodss.connect()Actively initiates a TCP server connection.
General Methodss.send()Sends TCP messages.
s.sendto()Sends UDP messages.
s.recv()Receives TCP messages.
s.recvfrom()Receives UDP messages.
s.close()Closes the socket, releasing all associated resources.
socket.gethostname()Returns the host name of the machine where the Python interpreter is currently executing.

This comprehensive overview provides a clear guide to establishing communication between a client and server using sockets, covering essential concepts and methods in Python's socket programming.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next