python-UDP协议

python-UDP协议

UDP协议:

        python中基于udp协议的客户端与服务端通信简单过程实现

  udp协议的一些特点(与tcp协议的比较)

       利用socketserver模块实现udp传输协议的并发通信

代码:

import optparse
from scapy.all import *
from scapy.layers.inet import IP, TCP


def Scan(ip):
    try:
        dport = random.randint(1, 65535)
        packet = IP(dst=ip) / TCP(dport=80)
        respone = sr1(packet, timeout=1.0, verbose=0)
        if respone:
            if int(respone[IP].proto) == 1:
                time.sleep(0.5)
                print(ip + ' ' + "is up")
            else:
                print(ip + ' ' + "is down")
        else:
            print(ip + ' ' + "is down")
    except:
        pass


def main():
    parser = optparse.OptionParser("Usage: %prog -i <ip address>")
    parser.add_option("-i", '--ip', type="string", dest="targetIP", help="specify the IP address")
    options, args = parser.parse_args()
    if '-' in options.targetIP:
        for i in range(int(options.targetIP.split('-')[0].split('.')[3]), int(options.targetIP.split('-')[1]) + 1):
            Scan(options.targetIP.split('.')[0] + '.' + options.targetIP.split('.')[1] + '.' +
                 options.targetIP.split('.')[2] + '.' + str(i))
    else:
        Scan(options.targetIP)


if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/qq_48985780/article/details/121877448