#!/usr/bin/python
###------------------------------------------------------------------
###    This code is a part of a tutorial for the Blue Seal Project 
###     created by Jiří Daněk (www.bluesealproject.com).
###    Please, do not remove this remark from the code.
###------------------------------------------------------------------

            # First, import necessary libraries
            import sys, SocketServer


            # We have to set the IP address of the computer and the port used
            HOST, PORT = "192.168.0.21", 9999	

            # Now, we define class and define the TCP/IP server within the class
            class MyTCPHandler(SocketServer.BaseRequestHandler):
                """
                The RequestHandler class for our server.
            
                It is instantiated once per connection to the server, and must
                override the handle() method to implement communication to the
                client.
                """

                def handle(self):
                    # self.request is the TCP socket connected to the client
                    self.data = self.request.recv(1024).strip()
		
	            # write out the IP address of the connected client
                    print("{} wrote:".format(self.client_address[0]))
        
	            # write out the message sent by the connected client decoded with UTF-8
	            print(self.data.decode("utf-8"))

	            """
                    Next command sends back received data, but the lower-cased, in the format
                    the BS Basic Controller can process and display
                    """
                    self.request.sendall("$(RTN=" + self.data.lower() + ")")



            # Create the server, binding to localhost on port 9999
            server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    
            # Start the main program
            if __name__ == "__main__":
    
                # Activate the server; this will keep running until you
                # interrupt the program with Ctrl-C

                server.serve_forever()