Installation method of 1:pycurl module
Install Pycurl
2: The example code below, is implemented under Python3, if the use of Python2 slightly modified can
#-*-coding:utf-8-*-ImportOs,sysImport TimeImportSYSImportPycurlurl="https://www.baidu.com"C=Pycurl. Curl () c.setopt (Pycurl. URL, url)#Connection Timeout time, 5 secondsC.setopt (Pycurl. ConnectTimeout, 5)#Download Timeout time, 5 secondsC.setopt (Pycurl. TIMEOUT, 5) c.setopt (Pycurl. Forbid_reuse,1) c.setopt (Pycurl. Maxredirs,1) c.setopt (Pycurl. Noprogress,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 ()exceptException 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("HTTP status code:%s"%(http_code))Print("DNS resolution Time:%.2f MS"% (namelookup_time*1000))Print("established connection time:%.2f Ms"% (connect_time*1000))Print("Ready for transfer time:%.2f MS"% (pretransfer_time*1000))Print("Transfer start time:%.2f Ms"% (starttransfer_time*1000))Print("total time of transmission end:%.2f MS"% (total_time*1000))Print("Download Packet size:%d bytes/s"%(size_download))Print("HTTP Header size:%d byte"%(header_size))Print("average download speed:%d bytes/s"%(Speed_download)) Indexfile.close () c.close ()
Probing Web service quality using Pycurl