python3 通过 pycurl 检测

发布时间:2019-09-25 08:13:22编辑:auto阅读(1488)

    由于python3 对StringIO 集成到io 模块中了,所有python3 import StringIO 改成了 import io,pycurl.Curl() 初始化对象C ,其 属性WRITEFUNCTION 要求传递参数是byte,b.write 需要初始化成io.BytesIO()

    #!/bin/env pyhon3
    import pycurl
    import io

    def check(urls):
    dict_data={}
    #if url not in errorurl:
    url = 'http://' + str(urls)
    dict_data['url']=url
    c = pycurl.Curl()
    c.setopt(pycurl.CONNECTTIMEOUT,5)
    c.setopt(pycurl.TIMEOUT,5)
    c.setopt(pycurl.NOPROGRESS,1)
    c.setopt(pycurl.FORBID_REUSE,1)
    c.setopt(pycurl.MAXREDIRS,3)
    c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)
    c.setopt(c.URL, url)
    try:
    b = io.BytesIO()
    c.setopt(c.WRITEFUNCTION, b.write)
    c.perform()
    HTTP_CODE = c.getinfo(pycurl.HTTP_CODE) #HTTP状态码
    dict_data['HTTP状态码'] = HTTP_CODE
    DNS_TIME = c.getinfo(c.NAMELOOKUP_TIME) 1000 #DNS消耗时间
    dict_data['DNS消耗时间'] = DNS_TIME
    CONNECT_TIME = c.getinfo(c.CONNECT_TIME)
    1000 #建连消耗时间
    dict_data['建连消耗时间'] = CONNECT_TIME
    PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME) 1000 #建连到准备传输消耗
    dict_data['建连到准备传输消耗'] = PRETRANSFER_TIME
    STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)
    1000 #建连到传输消耗
    dict_data['建连到传输消耗'] = STARTTRANSFER_TIME
    TOTAL_TIME = c.getinfo(c.TOTAL_TIME) * 1000 #传输结束消耗时间
    dict_data['传输结束消耗时间'] = TOTAL_TIME
    SPEED_DOWNLOAD = c.getinfo(c.SPEED_DOWNLOAD) #平均下载速度
    dict_data['平均下载速度'] = SPEED_DOWNLOAD
    b.close()
    c.close()
    except pycurl.error:
    pass
    #errno, errstr = error
    #print 'An error occurred: ', errstr
    for var in dict_data.keys():
    print(var,':',dict_data[var])
    website=input('请输入检测站点域名:如www.baidu.com')
    check(website)

关键字