def checksum(source_string):
    """
    I'm not too confident that this is right but testing seems
    to suggest that it gives the same answers as in_cksum in ping.c
    """
    sum = 0
    countTo = (len(source_string) / 2) * 2
    count = 0
    while count < countTo:
        v1 = source_string[count + 1]
        if not isinstance(v1, int):
            v1 = ord(v1)
        v2 = source_string[count]
        if not isinstance(v2, int):
            v2 = ord(v2)
        thisVal = v1 * 256 + v2
        sum = sum + thisVal
        sum = sum & 0xffffffff  # Necessary?
        count = count + 2

    if countTo < len(source_string):
        sum = sum + ord(source_string[len(source_string) - 1])
        sum = sum & 0xffffffff  # Necessary?

    sum = (sum >> 16) + (sum & 0xffff)
    sum = sum + (sum >> 16)
    answer = ~sum
    answer = answer & 0xffff

    # Swap bytes. Bugger me if I know why.
    answer = answer >> 8 | (answer << 8 & 0xff00)

    return answer

    I'm not too confident that this is right but testing seems
    to suggest that it gives the same answers as in_cksum in ping.c
    
bin/ping.py