Python sockets with threads

Multithreaded socket server in Python

A thread is a sequence of such instructions within a program that can be executed independently of other code. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution. Multithreaded Socket Programming describes that a Multithreaded Socket Server can communicate with more than one client at the same time in the same network.

In the previous lesson Python Socket Programming describes a Server Socket Program can communicate with only one client at a time . That means, the Python Server Socket Program does not accept more than one Client connection . From the following section you can understand how to a Python Multithreaded Server M can communicate with more than one Clients at the same time . You can see the basics of Socket Programming in the previous lesson , before you start this section take a look at Python Socket Programming

Python Multithreaded Socket Programming has two sections:

  1. Python Multi Threaded Server Socket Program (Server.py)
  2. Python Client Socket Program (client.py)

import socket, threading class ClientThread(threading.Thread): def __init__(self,clientAddress,clientsocket): threading.Thread.__init__(self) self.csocket = clientsocket print («New connection added: «, clientAddress) def run(self): print («Connection from : «, clientAddress) #self.csocket.send(bytes(«Hi, This is from Server..»,’utf-8′)) msg = » while True: data = self.csocket.recv(2048) msg = data.decode() if msg==’bye’: break print («from client», msg) self.csocket.send(bytes(msg,’UTF-8′)) print («Client at «, clientAddress , » disconnected. «) LOCALHOST = «127.0.0.1» PORT = 8080 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind((LOCALHOST, PORT)) print(«Server started») print(«Waiting for client request..») while True: server.listen(1) clientsock, clientAddress = server.accept() newthread = ClientThread(clientAddress, clientsock) newthread.start()

Читайте также:  Java version on class files

import socket SERVER = «127.0.0.1» PORT = 8080 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((SERVER, PORT)) client.sendall(bytes(«This is from Client»,’UTF-8′)) while True: in_data = client.recv(1024) print(«From Server :» ,in_data.decode()) out_data = input() client.sendall(bytes(out_data,’UTF-8′)) if out_data==’bye’: break client.close()

How to run this program ?

Create Python Multi Threaded Server Socket Program (Server.py) and Python Client Socket Program (client.py) in two separate files. Open a DOS prompt (console) and run the Server Program first. Then you will get the message «Server started» in Server side. Next you start the Client program in another DOS prompt (console), then you can see the message from Server . Like this , you can start more than one client at the same time from different Dos prompts and communicate with the Server program. The server accept your message and reply the same message to the same client. You can open many client program and test the server reply to each client.

Источник

Оцените статью