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.

36 lines
1008 B

  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. if __name__ == "__main__":
  20. HOST, PORT = "localhost", 9999
  21. # Init the TCP server object, bind it to the localhost on 9999 port
  22. tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer)
  23. # Activate the TCP server.
  24. # To abort the TCP server, press Ctrl-C.
  25. tcp_server.serve_forever()