最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python構(gòu)造icmp echo請求和實(shí)現(xiàn)網(wǎng)絡(luò)探測器功能代碼分享

 更新時間:2014年01月10日 10:26:12   作者:  
本文分享了二個python示例,python構(gòu)造icmp echo請求、實(shí)現(xiàn)網(wǎng)絡(luò)探測器功能代碼,類似nmap功能

python發(fā)送icmp echo requesy請求

復(fù)制代碼 代碼如下:

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')



掃描探測網(wǎng)絡(luò)功能(網(wǎng)絡(luò)探測器)

復(fù)制代碼 代碼如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
探測網(wǎng)絡(luò)主機(jī)存活。
'''

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

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

    參數(shù):
        ipPool      -- 可迭代的IP地址池
        icmpPacket  -- 構(gòu)造的icmp報(bào)文
        icmpSocket  -- icmp套字接
        timeout     -- 設(shè)置發(fā)送超時
    '''
    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 )

    def run(self):
        time.sleep(0.01)  #等待接收線程啟動
        for ip in self.ipPool:
            try:
                self.Sock.sendto(self.packet, (ip, 0))
            except socket.timeout:
                break
        time.sleep(self.timeout)

class Nscan:
    '''
    參數(shù):
        timeout    -- Socket超時,默認(rèn)3秒
        IPv6       -- 是否是IPv6,默認(rèn)為False
    '''
    def __init__(self, timeout=3, IPv6=False):
        self.timeout = timeout
        self.IPv6 = IPv6

        self.__data = struct.pack('d', time.time())   #用于ICMP報(bào)文的負(fù)荷字節(jié)(8bit)
        self.__id = os.getpid()   #構(gòu)造ICMP報(bào)文的ID字段,無實(shí)際意義

    @property   #屬性裝飾器
    def __icmpSocket(self):
        '''創(chuàng)建ICMP Socket'''
        if not self.IPv6:
            Sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.getprotobyname("icmp"))
        else:
            Sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.getprotobyname("ipv6-icmp"))
        return Sock

    def __inCksum(self, packet):
        '''ICMP 報(bào)文效驗(yàn)和計(jì)算方法'''
        if len(packet) & 1:
            packet = packet + '\0'
        words = array.array('h', packet)
        sum = 0
        for word in words:
            sum += (word & 0xffff)
        sum = (sum >> 16) + (sum & 0xffff)
        sum = sum + (sum >> 16)

        return (~sum) & 0xffff

    @property
    def __icmpPacket(self):
        '''構(gòu)造 ICMP 報(bào)文'''
        if not self.IPv6:
            header = struct.pack('bbHHh', 8, 0, 0, self.__id, 0) # TYPE、CODE、CHKSUM、ID、SEQ
        else:
            header = struct.pack('BbHHh', 128, 0, 0, self.__id, 0)

        packet = header + self.__data     # packet without checksum
        chkSum = self.__inCksum(packet) # make checksum

        if not self.IPv6:
            header = struct.pack('bbHHh', 8, 0, chkSum, self.__id, 0)
        else:
            header = struct.pack('BbHHh', 128, 0, chkSum, self.__id, 0)

        return header + self.__data   # packet *with* checksum

    def isUnIP(self, IP):
        '''判斷IP是否是一個合法的單播地址'''
        IP = [int(x) for x in IP.split('.') if x.isdigit()]
        if len(IP) == 4:
            if (0 < IP[0] < 223 and IP[0] != 127 and IP[1] < 256 and IP[2] < 256 and 0 < IP[3] < 255):
                return True
        return False

    def makeIpPool(self, startIP, lastIP):
        '''生產(chǎn) IP 地址池'''
        IPver = 6 if self.IPv6 else 4
        intIP = lambda ip: IPy.IP(ip).int()
        ipPool = {IPy.intToIp(ip, IPver) for ip in range(intIP(startIP), intIP(lastIP)+1)}

        return {ip for ip in ipPool if self.isUnIP(ip)}

    def mPing(self, ipPool):
        '''利用ICMP報(bào)文探測網(wǎng)絡(luò)主機(jī)存活

        參數(shù):
            ipPool  -- 可迭代的IP地址池
        '''
        Sock = self.__icmpSocket
        Sock.settimeout(self.timeout)
        packet = self.__icmpPacket
        recvFroms = set()   #接收線程的來源IP地址容器

        sendThr = SendPingThr(ipPool, packet, Sock, self.timeout)
        sendThr.start()

        while True:
            try:
                recvFroms.add(Sock.recvfrom(1024)[1][0])
            except Exception:
                pass
            finally:
                if not sendThr.isAlive():
                    break
        return recvFroms & ipPool

if __name__=='__main__':
    s = Nscan()
    ipPool = s.makeIpPool('192.168.0.1', '192.168.0.254')
    print( s.mPing(ipPool) )

相關(guān)文章

  • 關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton

    關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton

    這篇文章主要介紹了關(guān)于Python?Tkinter復(fù)選框Checkbutton,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組

    python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組

    這篇文章主要介紹了python如何將txt文件的內(nèi)容逐行讀取轉(zhuǎn)化成數(shù)組問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • python中的__dict__屬性介紹

    python中的__dict__屬性介紹

    這篇文章主要介紹了python中的__dict__屬性介紹,首先通過將字典轉(zhuǎn)換成對象的小技巧,展開標(biāo)題介紹,具有一定的參考價值,下文具體的相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • Python實(shí)現(xiàn)病毒仿真器的方法示例(附demo)

    Python實(shí)現(xiàn)病毒仿真器的方法示例(附demo)

    這篇文章主要介紹了Python實(shí)現(xiàn)病毒仿真器的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 利用Python+PyQt5實(shí)現(xiàn)簡易瀏覽器的實(shí)戰(zhàn)記錄

    利用Python+PyQt5實(shí)現(xiàn)簡易瀏覽器的實(shí)戰(zhàn)記錄

    這篇文章主要給大家介紹了關(guān)于如何利用Python+PyQt5實(shí)現(xiàn)簡易瀏覽器的相關(guān)資料,Qt 的主要優(yōu)勢是可以開發(fā)跨平臺的圖形界面程序,基于 Qt 的應(yīng)用能夠借助于各平臺的原生性在不同類的設(shè)備上運(yùn)行,而無須修改任何代碼庫,需要的朋友可以參考下
    2021-07-07
  • pandas中聚合函數(shù)agg的具體用法

    pandas中聚合函數(shù)agg的具體用法

    Pandas中的的agg()函數(shù)為aggregate的縮寫.總數(shù)、合計(jì)、聚合的意思.是一個功能非常強(qiáng)大的函數(shù).在Pandas中可以利用agg()對Series、DataFrame以及groupby()后的結(jié)果進(jìn)行聚合操作,下面這篇文章主要給大家介紹了關(guān)于pandas中聚合函數(shù)agg的具體用法,需要的朋友可以參考下
    2022-07-07
  • Python如何生成隨機(jī)數(shù)及random隨機(jī)數(shù)模塊應(yīng)用

    Python如何生成隨機(jī)數(shù)及random隨機(jī)數(shù)模塊應(yīng)用

    這篇文章主要介紹了Python如何生成隨機(jī)數(shù)及random隨機(jī)數(shù)模塊應(yīng)用,首先我們要知道在python中用于生成隨機(jī)數(shù)的模塊是random,在使用前需要import。由此展開內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-06-06
  • 如何構(gòu)建第二個Django的應(yīng)用程序

    如何構(gòu)建第二個Django的應(yīng)用程序

    這篇文章主要介紹了如何構(gòu)建第二個Django的應(yīng)用程序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Python的Django框架中從url中捕捉文本的方法

    Python的Django框架中從url中捕捉文本的方法

    這篇文章主要介紹了Python的Django框架中從url中捕捉文本的方法,以及URLconf搜索的一些相關(guān)情況,需要的朋友可以參考下
    2015-07-07
  • Python list列表中刪除多個重復(fù)元素操作示例

    Python list列表中刪除多個重復(fù)元素操作示例

    這篇文章主要介紹了Python list列表中刪除多個重復(fù)元素操作,結(jié)合實(shí)例形式分析了Python刪除list列表重復(fù)元素的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-02-02

最新評論

壶关县| 莒南县| 金寨县| 达日县| 东源县| 大姚县| 河北区| 南投县| 信丰县| 进贤县| 富阳市| 江陵县| 全椒县| 四平市| 左权县| 施秉县| 双牌县| 无棣县| 大兴区| 安化县| 平阳县| 雷波县| 河津市| 蒙阴县| 贵州省| 田东县| 大同市| 彭山县| 吉林市| 申扎县| 泰顺县| 全南县| 紫阳县| 青冈县| 开阳县| 永宁县| 夏津县| 宁乡县| 布尔津县| 东源县| 镇原县|