• python 通过 http、dns、icmp判断网络状态


    #http使用requests发包bs4解析,dns、icmp 使用scapy发包
    import
    time import threading import requests,bs4 from scapy.all import * from requests.exceptions import ReadTimeout,ConnectTimeout,HTTPError,ConnectionError import struct def send_http_packet(url): requests.packages.urllib3.disable_warnings() user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" headers = {'User-Agent': user_agent} url = "http://" + url response_html= "" try: response = requests.get(url, headers) response_html = response.content.decode() return response_html except ReadTimeout: print('Read Timeout') return None except ConnectTimeout: print('Connect Timeout') return None except HTTPError: print('HTTP Error') return None except ConnectionError: print('Connection Error') return None def check_http(html,title): if html != None and title != None : soup = bs4.BeautifulSoup(html, 'lxml') html_title = "" html_title = soup.title.text if title in html_title: return True else: return None else: print('html or title is None') return None def shend_icmp_packet(ip_address): request_time = time.time() request_time = struct.pack('>d',request_time) response_packet = sr1(IP(dst=ip_address)/ICMP()/request_time,timeout=1,verbose = False) return response_packet def icmp_relay_time(ip_address): response_packet = shend_icmp_packet(ip_address) relay_time = time.time() if response_packet != None : response_time = response_packet.getlayer(Raw).load response_time = struct.unpack('>d',response_time) relay_time = (relay_time - response_time[0])*1000 return relay_time else: return None def real_icmp_relay_time(gateway,ip_address): time_ipadd = icmp_relay_time(ip_address) time_gateway = icmp_relay_time(gateway) if time_ipadd == None or time_gateway == None: return None else: return time_ipadd - time_gateway def send_dns_packet(domainname,dnsserver): i = IP(dst=dnsserver) u = UDP(dport = 53) d = DNS(rd=1) d.qd = DNSQR(qname = domainname , qtype = 1 ,qclass = 1) dns_request_packet = i/u/d dns_response_packet = sr1(dns_request_packet,timeout=2,verbose = False) if dns_response_packet != None: dns_response_packet = dns_response_packet.getlayer(DNS).fields if dns_response_packet['rcode'] == 0 : return dns_response_packet elif dns_response_packet['rcode'] == 3 : print("Domain Name Error") return None elif dns_response_packet['rcode'] == 2 : print("Server Inside Failure") return None else: print("dns server no response") return None def main(): # res = send_http_packet("www.baidu.com") # print(check_http(res,"百度")) res = real_icmp_relay_time("172.17.1.1","192.168.101.2") print(res) # res = send_dns_packet("www.baidu.com","114.114.114.144") # print(res) if __name__ == '__main__': main()
  • 相关阅读:
    多线程编程总结:三、Task的使用和意义
    C#知识点总结系列:5、CLR的组成和运转
    TFS二次开发系列:五、工作项查询
    极速理解设计模式系列【目录索引】
    C#知识点总结系列:1、C#中Hashtable、Dictionary详解以及写入和读取对比
    Silverlight实用窍门大集合+Silverlight 5 最全新特性【目录索引】
    CodeUI Test:UIMap录制文件分析一
    Windows 8实用窍门系列【目录索引】
    Audacity开源音频处理软件使用入门
    python3使用mutagen进行音频元数据处理
  • 原文地址:https://www.cnblogs.com/yyxianren/p/10751370.html
Copyright © 2020-2023  润新知