Loesome

Untitled

Jan 23rd, 2021 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import socket, threading
  2.  
  3.  
  4. class ClientThread(threading.Thread):
  5. def __init__(self, clientAddress, clientsocket):
  6. threading.Thread.__init__(self)
  7. self.csocket = clientsocket
  8. print("New connection added: ", clientAddress)
  9.  
  10. def run(self):
  11. print("Connection from : ", clientAddress)
  12. # self.csocket.send(bytes("Hi, This is from Server..",'utf-8'))
  13. msg = ''
  14. while True:
  15. data = self.csocket.recv(2048)
  16. msg = data.decode()
  17. if msg == 'bye':
  18. break
  19. print("from client", msg)
  20. self.csocket.send(bytes(msg, 'UTF-8'))
  21. print("Client at ", clientAddress, " disconnected...")
  22.  
  23.  
  24. LOCALHOST = "127.0.0.1"
  25. PORT = 5000
  26. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  27. server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  28. server.bind((LOCALHOST, PORT))
  29. print("Server started")
  30. print("Waiting for client request..")
  31. while True:
  32. server.listen(1)
  33. clientsock, clientAddress = server.accept()
  34. newthread = ClientThread(clientAddress, clientsock)
  35. newthread.start()
  36.  
Add Comment
Please, Sign In to add comment