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.

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