python3 通过 pycurl 检测站点性能,各个环节返回时间
2021-06-29 17:04
标签:时间 bytesio data 通过 name stringio out pass 要求 #!/bin/env pyhon3 def check(urls): python3 通过 pycurl 检测站点性能,各个环节返回时间 标签:时间 bytesio data 通过 name stringio out pass 要求 原文地址:http://blog.51cto.com/1884884/2175072
import pycurl
import io
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)
下一篇:扩展样式表语言-XSL
文章标题:python3 通过 pycurl 检测站点性能,各个环节返回时间
文章链接:http://soscw.com/index.php/essay/99456.html