欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

python实现ping的方法

程序员文章站 2022-05-18 18:39:51
...
本文实例讲述了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 dest_addr<. dest_addr="socket.gethostbyname(dest_addr)" header is type code checksum id sequence my_checksum="0" make a dummy heder with checksum. icmp_echo_request struct.unpack test bytesindouble='struct.calcsize("d")' data="(192" time.time calculate the on and header. now that we have right put in. it just easier to up new than stuff into dummy. socket.htons packet="header" my_socket.sendto don know about def do_one timeout returns either delay seconds or none timeout. icmp='socket.getprotobyname("icmp")' try: my_socket="socket.socket(socket.AF_INET," socket.sock_raw except socket.error msg if errno="=" operation not permitted note messages can only be sent from processes running as root. raise original error my_id="os.getpid()" send_one_ping my_socket.close return verbose_ping count="100):" send>countdest_addrtimeout

希望本文所述对大家的Python程序设计有所帮助。

python实现ping的方法

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频