python3 通过 pycurl 检测站点性能,各个环节返回时间

2021-06-29 17:04

阅读:471

标签:时间   bytesio   data   通过   name   stringio   out   pass   要求   

由于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)

python3 通过 pycurl 检测站点性能,各个环节返回时间

标签:时间   bytesio   data   通过   name   stringio   out   pass   要求   

原文地址:http://blog.51cto.com/1884884/2175072


评论


亲,登录后才可以留言!