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)
msg = ''
while True:
data = self.csocket.recv(2048)
msg = data.decode()
if msg == 'bye':
break
print ("from client", msg)
self.csocket.sendall(bytes(msg,'UTF-8'))
LOCALHOST = "localhost"
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(3)
clientsock, clientAddress = server.accept()
newthread = ClientThread(clientAddress, clientsock)
newthread.start()