Socket Programming with Multi-threading in Python?


Multithreading Concepts

Multithreading is the core concept of nearly all modern programming languages especially python because of its simplistic implementation of threads.

A thread is a sub-program within a program that can be executed independently of other section of the code. A thread executes in the same context sharing program’s runnable resources like memory.

When in a single process, we are executing multiple threads simultaneously, it is called multithreading.

Python Multithreading Modules for a thread implementation

To implements threads in programs, python provides two modules −

  • thread (for python 2.x) or _thread(for python 3.x) module
  • threading module

Where the thread module creates a thread as a function whereas the threading module provides an object-oriented approach to create a thread.

Syntax

_thread.start_new_thread(func, args[, kwargs])

Above starts a new thread and returns its identifier. The first argument is a function func which the thread executes with the second argument containing a tuple with a positional list of arguments. The optional kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exists.

In this, we see a basic example of a client-server application. Where clients basically open a socket connection and send queries to the server. The server responds back.

On running with no argument, this program starts with a TCP socket server that listens for connections to 127.0.0.1 on port 8000.

client_thread1.py

import socket
import sys
def main():
   soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   host = "127.0.0.1"
   port = 8000
   try:
      soc.connect((host, port))
   except:
      print("Connection Error")
      sys.exit()
   print("Please enter 'quit' to exit")
   message = input(" -> ")
   while message != 'quit':
      soc.sendall(message.encode("utf8"))
      if soc.recv(5120).decode("utf8") == "-":
         pass # null operation
      message = input(" -> ")
   soc.send(b'--quit--')
if __name__ == "__main__":
   main()

Whereas the server program is,

server_thread1.py

import socket
import sys
import traceback
from threading import Thread
def main():
   start_server()
def start_server():
   host = "127.0.0.1"
   port = 8000 # arbitrary non-privileged port
   soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   print("Socket created")
   try:
      soc.bind((host, port))
   except:
      print("Bind failed. Error : " + str(sys.exc_info()))
      sys.exit()
   soc.listen(6) # queue up to 6 requests
   print("Socket now listening")
   # infinite loop- do not reset for every requests
   while True:
      connection, address = soc.accept()
      ip, port = str(address[0]), str(address[1])
      print("Connected with " + ip + ":" + port)
   try:
      Thread(target=client_thread, args=(connection, ip, port)).start()
   except:
      print("Thread did not start.")
      traceback.print_exc()
   soc.close()
def clientThread(connection, ip, port, max_buffer_size = 5120):
   is_active = True
   while is_active:
      client_input = receive_input(connection, max_buffer_size)
      if "--QUIT--" in client_input:
         print("Client is requesting to quit")
         connection.close()
         print("Connection " + ip + ":" + port + " closed")
         is_active = False
      else:
         print("Processed result: {}".format(client_input))
         connection.sendall("-".encode("utf8"))
def receive_input(connection, max_buffer_size):
   client_input = connection.recv(max_buffer_size)
   client_input_size = sys.getsizeof(client_input)
   if client_input_size > max_buffer_size:
      print("The input size is greater than expected {}".format(client_input_size))
   decoded_input = client_input.decode("utf8").rstrip()
   result = process_input(decoded_input)
   return result
def process_input(input_str):
   print("Processing the input received from client")
   return "Hello " + str(input_str).upper()
if __name__ == "__main__":
   main()

On running above script, run the server_thread1.py in the terminal as,

python server_thread1.py
Socket created
Socket now listening

We will watch the server window and understand the flow. Now open multiple clients terminal, run client thread

python client_thread1.py
Enter 'quit' to exit
-> Zack
->

In another terminal, run another client program & watch server terminal window too,

python client_thread1.py
Enter 'quit' to exit
-> Python
-> quit

Another terminal, run client thread,

python client_thread1.py
Enter 'quit' to exit
-> world!
-> Anothny
->

And we can see our server window will display output something like,

Socket created
Socket now listening
Connected with 127.0.0.1:50275
Processing the input received from client
Processed result: Hello ZACK
Connected with 127.0.0.1:50282
Processing the input received from client
Processed result: Hello PYTHON
Processing the input received from client
Client is requesting to quit
Connection 127.0.0.1:50282 closed
Connected with 127.0.0.1:50285
Processing the input received from client
Processed result: Hello WORLD!
Processing the input received from client
Processed result: Hello ANOTHNY

So threads provide one of the most common technique to handle multiple socket connection and clients.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements