You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

  1. import socketserver
  2. class Handler_TCPServer(socketserver.BaseRequestHandler):
  3. """
  4. The TCP Server class for demonstration.
  5. Note: We need to implement the Handle method to exchange data
  6. with TCP client.
  7. """
  8. def handle(self):
  9. # self.request - TCP socket connected to the client
  10. self.data = self.request.recv(1024).strip()
  11. print("{} sent:".format(self.client_address[0]))
  12. print(self.data)
  13. # just send back ACK for data arrival confirmation
  14. self.request.sendall("ACK from TCP Server".encode())
  15. def ping(self, param):
  16. return "PONG"
  17. def command_pass(self, param):
  18. return param == self.password
  19. def command_nickname(self, nickname):
  20. if nickname not in self.user_list:
  21. self.user_list.append(nickname)
  22. return True
  23. else:
  24. return False
  25. if __name__ == "__main__":
  26. HOST, PORT = "localhost", 9999
  27. # Init the TCP server object, bind it to the localhost on 9999 port
  28. tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer)
  29. # Activate the TCP server.
  30. # To abort the TCP server, press Ctrl-C.
  31. tcp_server.serve_forever()