国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python構造icmp echo請求和實現網絡探測器功能代碼分享

2020-02-23 05:05:06
字體:
來源:轉載
供稿:網友

python發送icmp echo requesy請求

代碼如下:
import socket
import struct

def checksum(source_string):
    sum = 0
    countTo = (len(source_string)/2)*2
    count = 0
    while count<countTo:
        thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])
        sum = sum + thisVal
        sum = sum & 0xffffffff
        count = count + 2
    if countTo<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

def ping(ip):
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, 1)
    packet = struct.pack(
            "!BBHHH", 8, 0, 0, 0, 0
    )
    chksum=checksum(packet)
    packet = struct.pack(
            "!BBHHH", 8, 0, chksum, 0, 0
    )
    s.sendto(packet, (ip, 1))

if __name__=='__main__':
    ping('192.168.41.56')


掃描探測網絡功能(網絡探測器)

代碼如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
探測網絡主機存活。
'''

import os
import struct
import array
import time
import socket
import IPy
import threading

class SendPingThr(threading.Thread):
    '''
    發送ICMP請求報文的線程。

    參數:
        ipPool      -- 可迭代的IP地址池
        icmpPacket  -- 構造的icmp報文
        icmpSocket  -- icmp套字接
        timeout     -- 設置發送超時
    '''
    def __init__(self, ipPool, icmpPacket, icmpSocket, timeout=3):
        threading.Thread.__init__(self)
        self.Sock = icmpSocket
        self.ipPool = ipPool
        self.packet = icmpPacket
        self.timeout = timeout
        self.Sock.settimeout( timeout + 3 )

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上杭县| 讷河市| 沂水县| 精河县| 呼和浩特市| 西安市| 镇康县| 双牌县| 娄烦县| 揭阳市| 隆尧县| 中江县| 盐津县| 承德县| 阜新| 区。| 乌鲁木齐市| 历史| 绵阳市| 新巴尔虎左旗| 乐安县| 武邑县| 丰县| 深州市| 济阳县| 平昌县| 公主岭市| 东乌珠穆沁旗| 定西市| 康保县| 仪征市| 刚察县| 开平市| 威远县| 罗定市| 曲水县| 松江区| 定结县| 泽普县| 保康县| 永福县|