Python Socket Programming

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will explore the world of socket programming in Python, set in the magical scenario of a vibrant carnival and featuring the whimsical character of a fantasy balloon artist. The carnival is bustling with excitement and energy, and the fantasy balloon artist enchants the audience with their spectacular creations. The goal of this lab is to dive into the realm of socket programming and weave our own magical network connections just like the mesmerizing balloon creations at the carnival.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/NetworkingGroup(["`Networking`"]) python/NetworkingGroup -.-> python/socket_programming("`Socket Programming`") subgraph Lab Skills python/socket_programming -.-> lab-271592{{"`Python Socket Programming`"}} end

Establishing Basic Client-Server Communication

In this step, we will create a simple client-server connection using Python socket programming. The client and server will communicate and exchange data in a basic interaction.

  1. Open a new Python file named server.py in the directory ~/project.
  2. Implement the server-side code in server.py.
## server.py
import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 8888))
server_socket.listen(1)

connection, address = server_socket.accept()
with connection:
    print('Connected by', address)
    while True:
        data = connection.recv(1024)
        if not data:
            break
        connection.sendall(data)
  1. Open another Python file named client.py in the directory ~/project.
  2. Implement the client-side code in client.py.
## client.py
import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 8888))
client_socket.sendall(b'Hello, server')
data = client_socket.recv(1024)
print('Received', repr(data))

Run these two files at the same time and click "Split Terminal" to achieve it. The information below should be displayed on your terminal:

labex:project/ $ python server.py
Connected by ('127.0.0.1', 40082)
labex:project/ $ python client.py
Received b'Hello, server'

Enhancing Data Serialization for Communication

In this step, we will enhance the communication between the client and server by serializing and deserializing data for transmission.

  1. Modify the server.py code to include data serialization and deserialization.
## server.py
import socket
import pickle

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 8888))
server_socket.listen(1)

connection, address = server_socket.accept()
with connection:
    print('Connected by', address)
    while True:
        data = connection.recv(1024)
        if not data:
            break
        deserialized_data = pickle.loads(data)
        processed_data = deserialized_data.upper()
        connection.sendall(processed_data.encode())
  1. Modify the client.py code to include data serialization and deserialization.
## client.py
import socket
import pickle

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 8888))
data_to_send = pickle.dumps('Hello, server')
client_socket.sendall(data_to_send)
received_data = client_socket.recv(1024)
print('Received', received_data.decode())

Run these two files at the same time and click "Split Terminal" to achieve it. The information below should be displayed on your terminal:

labex:project/ $ python server.py
Connected by ('127.0.0.1', 40082)
labex:project/ $ python client.py
Received HELLO, SERVER

Summary

In this lab, we delved into the enchanting realm of Python socket programming, drawing inspiration from the magical carnival atmosphere and the fantastical balloon artist's creations. By guiding you through the steps of creating client-server connections, enhancing communication with data serialization, and more, we aimed to provide an immersive and educational experience, empowering you to master the art of socket programming and delight in the wondrous connections they can weave.

Other Python Tutorials you may like