Socket Programming with Multi-threading in Python

Prerequisite : Socket Programming in Python, Multi-threading in Python

Socket Programming-> It helps us to connect a client to a server. Client is message sender and receiver and server is just a listener that works on data sent by client.

What is a Thread?
A thread is a light-weight process that does not require much memory overhead, they are cheaper than processes.

What is Multi-threading Socket Programming?
Multithreading is a process of executing multiple threads simultaneously in a single process.

Multi-threading Modules :
A _thread module & threading module is used for multi-threading in python, these modules help in synchronization and provide a lock to a thread in use.

from _thread import *
import threading

A lock object is created by->

print_lock = threading.Lock()

A lock has two states, “locked” or “unlocked”. It has two basic methods acquire() and release(). When the state is unlocked print_lock.acquire() is used to change state to locked and print_lock.release() is used to change state to unlock.

The function thread.start_new_thread() is used to start a new thread and return its identifier. The first argument is the function to call and its second argument is a tuple containing the positional list of arguments.

Let’s study client-server multithreading socket programming by code-
Note:-The code works with python3.

Multi-threaded Server Code

# import socket programming library
import socket
# import thread module
from _thread import *
import threading
print_lock = threading.Lock()
# thread fuction
def threaded(c):
    while True:
        # data received from client
        data = c.recv(1024)
        if not data:
            # lock released on exit
        # reverse the given string from client
        data = data[::-1]
        # send back reversed string to client
    # connection closed
def Main():
    host = ""
    # reverse a port on your computer
    # in our case it is 12345 but it
    # can be anything
    port = 12345
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host, port))
    print("socket binded to post", port)
    # put the socket into listening mode
    print("socket is listening")
    # a forever loop until client wants to exit
    while True:
        # establish connection with client
        c, addr = s.accept()
        # lock acquired by client
        print('Connected to :', addr[0], ':', addr[1])
        # Start a new thread and return its identifier
        start_new_thread(threaded, (c,))
if __name__ == '__main__':

Console Window:
socket binded to post 12345
socket is listening
Connected to : : 11600

Client Code

# Import socket module
import socket
def Main():
    # local host IP ''
    host = ''
    # Define the port on which you want to connect
    port = 12345
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    # connect to server on local computer
    # message you send to server
    message = "shaurya says geeksforgeeks"
    while True:
        # message sent to server
        # messaga received from server
        data = s.recv(1024)
        # print the received message
        # here it would be a reverse of sent message
        print('Received from the server :',str(data.decode('ascii')))
        # ask the client whether he wants to continue
        ans = input(' Do you want to continue(y/n) :')
        if ans == 'y':
    # close the connection
if __name__ == '__main__':

Console Window:
Received from the server : skeegrofskeeg syas ayruahs

Do you want to continue(y/n) :y
Received from the server : skeegrofskeeg syas ayruahs

Do you want to continue(y/n) :n

Process finished with exit code 0


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This article is attributed to GeeksforGeeks.org

leave a comment



load comments

Subscribe to Our Newsletter