一、运行环境
1、Windows 10
2、python 3.8
二、安装第三方库pycurl
1、先安装
pip install wheel
2、在安装pycurl
https://download.lfd.uci.edu/pythonlibs/t7epjj8p/pycurl-7.43.0.3-cp38-cp38-win_amd64.whl
三、测试脚本
1 import pycurl 2 import os,sys 3 import time 4 import sys 5 6 7 try: 8 URL=sys.argv[1] # 测试网站的域名 9 except Exception as e: 10 print ("Error:"+str(e)) 11 print ("用法:请输入要探测的web地址") 12 sys.exit() 13 #URL="http://www.baidu.com" #测试网站的域名 14 c = pycurl.Curl() 15 c.setopt(pycurl.URL, URL) 16 c.setopt(pycurl.CONNECTTIMEOUT, 5) 17 c.setopt(pycurl.TIMEOUT, 5) 18 c.setopt(pycurl.NOPROGRESS, 1) 19 c.setopt(pycurl.FORBID_REUSE, 1) 20 c.setopt(pycurl.MAXREDIRS, 1) 21 c.setopt(pycurl.DNS_CACHE_TIMEOUT,30) 22 23 indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt", "wb") 24 c.setopt(pycurl.WRITEHEADER, indexfile) 25 c.setopt(pycurl.WRITEDATA, indexfile) 26 try: 27 c.perform() #提交请求 28 except Exception as e: 29 print("connecion error:"+str(e)) 30 indexfile.close() 31 c.close() 32 sys.exit() 33 34 NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME) 35 CONNECT_TIME = c.getinfo(c.CONNECT_TIME) 36 PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME) 37 STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME) 38 39 TOTAL_TIME = c.getinfo(c.TOTAL_TIME) 40 HTTP_CODE = c.getinfo(c.HTTP_CODE) 41 SIZE_DOWNLOAD = c.getinfo(c.SIZE_DOWNLOAD) 42 HEADER_SIZE = c.getinfo(c.HEADER_SIZE) 43 SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD) 44 45 print("测试网站:",URL) 46 print("HTTP状态码:{}" .format(HTTP_CODE)) 47 print("HTTP状态码:%s" %(HTTP_CODE)) 48 print("DNS解析时间:%.2f ms"%(NAMELOOKUP_TIME*1000)) 49 print("建立连接时间:%.2f ms" %(CONNECT_TIME*1000)) 50 print("准备传输时间:%.2f ms" %(PRETRANSFER_TIME*1000)) 51 print("传输开始时间:%.2f ms" %(STARTTRANSFER_TIME*1000)) 52 print("传输结束总时间:%.2f ms" %(TOTAL_TIME*1000)) 53 print("下载数据包大小:%d bytes/s" %(SIZE_DOWNLOAD)) 54 print("HTTP头部大小:%d byte" %(HEADER_SIZE)) 55 print("平均下载速度:%d bytes/s" %(SPEED_DOWNLOAD)) 56 #关闭文件及Curl对象 57 indexfile.close() 58 c.close()
四、验证
来源:https://www.cnblogs.com/ccip-ma/p/12033566.html