Ejemplo n.º 1
0
    def clientLoop(self):
        try:
            """
            Data variables for manipulating packets and handling them
            """
            data = Packet("", 0, "")
            hdata = ""
            mdata = ""

            while (self.connection):
                # if the user quits
                if (mdata.lower() == "quit" or data.msg.lower() == "quit"):
                    self.connection = False
                    self.close()
                    break
                # else send the message
                else:
                    hdata = str(input("Header (CMD, MSG, DAT): ")).upper()
                    while (not data.checkHeader(hdata)):
                        print("\n\nCLIENT >> Can only have MSG, CMD, or DAT type packets")
                        hdata = str(input("Header (CMD, MSG, DAT): "))
                    
                    mdata = str(input("Message: "))
                    while (not data.checkMessage(mdata)):
                        print("CLIENT >> Message must be less than 256 characters (bytes)")
                        mdata = str(input("Message: "))
                    
                    data.packetReformat(hdata, mdata)
                    self.send(data.packetFormat())
                    print("CLIENT >> Sending Data >> %s\t%d\t%s" % (data.header, data.size, data.msg))
        
        except socket.error as err:
            print("CLIENT >> Broken connection with %s on port %d" % (self.addr, self.port))
            print(err)
        
        finally:
            self.connection = False
            self.close()