Use string formatting : msglen = '%16s' %len(testmessage) will return a 16-byte string, beginning with spaces. Send it over your connection and use int() to get the message length Pierre