def netstring(fObj, *strings): for s in strings: fObj.write('%d:%s\n' % (len(s), s)) def netstringparser(netstrings): if netstrings.startswith('\r\n'): netstrings = netstrings.replace('\r\n', '\n') position = 1 while True: lengthEnd = netstrings.find(':', position) if lengthEnd == -1: break length = int(netstrings[position:lengthEnd]) yield netstrings[lengthEnd + 1:lengthEnd + 1 + length] position = lengthEnd + length + 2