python3.9测试网站网速

一、运行环境

1、Windows 10

2、python 3.9

二、安装第三方库pycurl

1、先安装

pip install wheel

2、在安装pycurl

https://download.lfd.uci.edu/pythonlibs/z4tqcw5k/pycurl-7.43.0.5-cp39-cp39-win_amd64.whl

三、测试脚本

import pycurl
import os,sys
import time
import sys


try:
    URL=sys.argv[1]    # 测试网站的域名
except Exception as e:
    print ("Error:"+str(e))
    print ("用法:请输入要探测的web地址")
    sys.exit()
#URL="http://www.baidu.com"    #测试网站的域名
c = pycurl.Curl()
c.setopt(pycurl.URL, URL)
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, 1)
c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)

indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt", "wb")
c.setopt(pycurl.WRITEHEADER, indexfile)
c.setopt(pycurl.WRITEDATA, indexfile)
try:
    c.perform()    #提交请求
except Exception as e:
    print("connecion error:"+str(e))
    indexfile.close()
    c.close()
    sys.exit()

NAMELOOKUP_TIME =  c.getinfo(c.NAMELOOKUP_TIME)
CONNECT_TIME =  c.getinfo(c.CONNECT_TIME)
PRETRANSFER_TIME =   c.getinfo(c.PRETRANSFER_TIME)
STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)

TOTAL_TIME = c.getinfo(c.TOTAL_TIME)
HTTP_CODE =  c.getinfo(c.HTTP_CODE)
SIZE_DOWNLOAD =  c.getinfo(c.SIZE_DOWNLOAD)
HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)

print("测试网站:",URL)
print("HTTP状态码:{}" .format(HTTP_CODE))
print("HTTP状态码:%s" %(HTTP_CODE))
print("DNS解析时间:%.2f ms"%(NAMELOOKUP_TIME*1000))
print("建立连接时间:%.2f ms" %(CONNECT_TIME*1000))
print("准备传输时间:%.2f ms" %(PRETRANSFER_TIME*1000))
print("传输开始时间:%.2f ms" %(STARTTRANSFER_TIME*1000))
print("传输结束总时间:%.2f ms" %(TOTAL_TIME*1000))
print("下载数据包大小:%d bytes/s" %(SIZE_DOWNLOAD))
print("HTTP头部大小:%d byte" %(HEADER_SIZE))
print("平均下载速度:%d bytes/s" %(SPEED_DOWNLOAD))
#关闭文件及Curl对象
indexfile.close()
c.close()

四、验证

未经允许不得转载:OZ分享-吉家大宝官方博客 » python3.9测试网站网速

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

置顶文章