print_lock threading.Lock()
def createThread(c, addr ):
print("new connection (addr}")
print (f" threadName: ", {threading.currentThread().getName()})
while True:
data = c.recv(1024)
print('Received from the client', str(data.decode('ascii')))
if not data:
print('Bye')
print_lock.release()
break
data = "Today we will learn about Multithreading with Socket programming"
c.send(data.encode('utf-8'))
c.close()
def Main():
host = "localhost"
port = 2022
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("socket binded to port", port)
s.listen()
print("Server is listening, Waiting for incoming ")
while True:
c, addr= s.accept()
print_lock.acquire()
print('Connected to ', addr[0], ':', addr[1])
print (f"threadName, before creating thread: ", {threading.currentThread().getName()}) thread threading. Thread (target=createThread, args=(c, addr))
thread.start()
s.close()
|