本文实例讲述了python实现ping的方法。分享给大家供大家参考。具体如下:
#!/usr/bin/env python
#coding:utf-8
import os, sys, socket, struct, select, time
# from /usr/include/linux/icmp.h; your milage may vary.
icmp_echo_request = 8 # seems to be the same on solaris.
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 16) + (sum & 0xffff)
sum = sum + (sum >> 16)
answer = ~sum
answer = answer & 0xffff
# swap bytes. bugger me if i know why.
answer = answer >> 8 | (answer