这是我在看《 Python 网络编程攻略》第 3.2 纯 Python 实现 Ping 程序代码,其中关于校验和算法部分代码看的吃力。代码如下:
def do_checksum(self, source_string):
""" Verify the packet integritity """
sum = 0
max_count = (len(source_string)/2)*2
count = 0
while count < max_count:
val = ord(source_string[count + 1])*256 + ord(source_string[count])
sum = sum + val
sum = sum & 0xffffffff
count = count + 2
if max_count<len(source_string):
sum = sum + ord(source_string[len(source_string) - 1])
sum = sum & 0xffffffff
sum = (sum >> 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
answer = ~sum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
不太明白的地方在:
大牛啊,请赐教!
1
mengzhuo 2016-06-02 16:19:01 +08:00
CRC32?
|
2
fcicq 2016-06-02 17:27:25 +08:00
RFC 1071 吧
|
3
mengzhuo 2016-06-02 18:49:10 +08:00
ls 正解
|