def checksum(source_string):
countTo = (int(len(source_string) / <NUM_LIT:2>)) * <NUM_LIT:2><EOL>sum = <NUM_LIT:0><EOL>count = <NUM_LIT:0><EOL>loByte = <NUM_LIT:0><EOL>hiByte = <NUM_LIT:0><EOL>while count < countTo:<EOL><INDENT>if (sys.byteorder == "<STR_LIT>"):<EOL><INDENT>loByte = source_string[count]<EOL>hiByte = source_string[count + <NUM_LIT:1>]<EOL><DEDENT>else:<EOL><INDENT>loByte = source_string[count + <NUM_LIT:1>]<EOL>hiByte = source_string[count]<EOL><DEDENT>try:     <EOL><INDENT>sum = sum + (hiByte * <NUM_LIT> + loByte)<EOL><DEDENT>except Exception:  <EOL><INDENT>sum = sum + (ord(hiByte) * <NUM_LIT> + ord(loByte))<EOL><DEDENT>count += <NUM_LIT:2><EOL><DEDENT>if countTo < len(source_string):  <EOL><INDENT>loByte = source_string[len(source_string) - <NUM_LIT:1>]<EOL>try:      <EOL><INDENT>sum += loByte<EOL><DEDENT>except Exception:   <EOL><INDENT>sum += ord(loByte)<EOL><DEDENT><DEDENT>sum &= <NUM_LIT><EOL>sum = (sum >> <NUM_LIT:16>) + (sum & <NUM_LIT>)    <EOL>sum += (sum >> <NUM_LIT:16>)                    <EOL>answer = ~sum & <NUM_LIT>                <EOL>answer = socket.htons(answer)<EOL>return answer<EOL>
A port of the functionality of in_cksum() from ping.c
Ideally this would act on the string as a series of 16-bit ints (host
packed), but this works.
Network data is big-endian, hosts are typically little-endian