python sockets multiclient handeling

Solutions on MaxInterview for python sockets multiclient handeling by the best coders in the world

showing results for - "python sockets multiclient handeling"
Lorenzo
13 Feb 2019
1
2import socket
3import os
4from _thread import *
5
6ServerSocket = socket.socket()
7host = '127.0.0.1'
8port = 1233
9ThreadCount = 0
10try:
11    ServerSocket.bind((host, port))
12except socket.error as e:
13    print(str(e))
14
15print('Waitiing for a Connection..')
16ServerSocket.listen(5)
17
18
19def threaded_client(connection):
20    connection.send(str.encode('Welcome to the Servern'))
21    while True:
22        data = connection.recv(2048)
23        reply = 'Server Says: ' + data.decode('utf-8')
24        if not data:
25            break
26        connection.sendall(str.encode(reply))
27    connection.close()
28
29while True:
30    Client, address = ServerSocket.accept()
31    print('Connected to: ' + address[0] + ':' + str(address[1]))
32    start_new_thread(threaded_client, (Client, ))
33    ThreadCount += 1
34    print('Thread Number: ' + str(ThreadCount))
35ServerSocket.close()
similar questions
queries leading to this page
python sockets multiclient handeling