[Python-au] Python Sockets

Garth Kidd garth@deadlybloodyserious.com
Wed, 22 Jan 2003 16:41:19 +1100


``rfind`` to the rescue:: 

    def recvCleanPacket(self, numBytes):
        "use instead of just using recv"
        inStr=self.request.recv(numBytes)
        if inStr and inStr[-1:] != '>': 
            return inStr[:inStr.rfind('>')+1]
        return inStr

My gut tells me that rfind is written in C, and likely to be a whole lot
faster. 

Some simple benchmarking should tell you whether it's more efficient to
skip the test for ending on a '>'; if you get a lot of nuls and
newlines, you may as well just:: 

    def recvCleanPacket(self, numBytes):
        "use instead of just using recv"
        inStr = self.request.recv(numBytes)
        return inStr[:inStr.rfind('>')+1]

Regards,
Garth.