### 进行https访问检测
body+="\r\n"
print("HTTPS连通性测试")
body+="HTTPS连通性测试"
body+="\r\n"

# https检测
import pycurl
from io import BytesIO

def get_https_timing(url):
    c = pycurl.Curl()
    c.setopt(c.URL, url)

    # 设置输出缓冲区
    buffer = BytesIO()
    c.setopt(c.WRITEDATA, buffer)
    c.setopt(c.SSL_VERIFYPEER, 0) # 禁用ssl证书验证
    c.setopt(c.SSL_VERIFYHOST, 0) # 禁用主机名检测

    # 执行请求
    c.perform()

    # 获取时间指标
    dns_lookup_time = c.getinfo(pycurl.NAMELOOKUP_TIME)
    connect_time = c.getinfo(pycurl.CONNECT_TIME)
    pretransfer_time = c.getinfo(pycurl.PRETRANSFER_TIME)
    starttransfer_time = c.getinfo(pycurl.STARTTRANSFER_TIME)
    total_time = c.getinfo(pycurl.TOTAL_TIME)

    c.close()

    return {
        'dns_lookup_time': dns_lookup_time,
        'connect_time': connect_time,
        'pretransfer_time': pretransfer_time,
        'starttransfer_time': starttransfer_time,
        'total_time': total_time
    }

# 要检查的URL
urlList = ['https://papi.baidu.com', 'https://newapi.baidu.com', 'https://static.baidu.com']
print(f"{'URL':30} {'DNS Lookup Time':20} {'Connect Time':20} {'Pretransfer Time':20} {'Starttransfer Time':20} {'Total Time':20}")
body+=f"{'URL':30} {'DNS Lookup Time':20} {'Connect Time':20} {'Pretransfer Time':20} {'Starttransfer Time':20} {'Total Time':20}"
body+="\r\n"
for url in urlList:
    # 获取HTTPS请求的时间指标
    timing = get_https_timing(url)
    # for key, value in timing.items():
    #     print(f"{key}: {value} seconds")
    print(f"{url:30} {timing['dns_lookup_time']:20.5f} {timing['connect_time']:20.5f} {timing['pretransfer_time']:20.5f} {timing['starttransfer_time']:20.5f} {timing['total_time']:20.5f}")
    body+=f"{url:30} {timing['dns_lookup_time']:20.5f} {timing['connect_time']:20.5f} {timing['pretransfer_time']:20.5f} {timing['starttransfer_time']:20.5f} {timing['total_time']:20.5f}"
    body += "\r\n"
最后修改:2024 年 05 月 11 日
如果觉得我的文章对你有用,请随意赞赏