1. 程式人生 > >pycurl測試網頁

pycurl測試網頁

pycurl

# -*- coding: UTF-8 -*-
import sys,os,time,pycurl


URL = input ("請輸出需要測試的網址:")


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.REDIRECT_TIME)
#STARTTRANSFRE_TIME = c.getinfo(c.STARTTRANSFRE_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("HTTP狀態碼: %s "%int(HTTP_CODE))
print("DNS解析時間:%.2f ms" %(NAMELOOKUP_TIME * 1000))
print("建立連接時間:%.2f ms"%(CONNECT_TIME * 1000))
print("準備傳輸時間:%.2f ms"%(PRETRANSFER_TIME * 1000))
#print ("傳輸開始時間:%.2f ms")%(STARTTRANSFRE_TIME * 1000)
print("傳輸結束時間:%.2f ms"%(TOTAL_TIME * 1000))
print("下載數據包大小:%d bytes/s"%(SIZE_DOWNLOAD))
print("HTTP頭部大小:%d byts"%(HEADER_SIZE))
print("平均下載速度:%d bytes/s"%(SPEED_DOWNLOAD))

indexfile.close()
c.close

pycurl測試網頁