python web service quality of detection (pycurl module)

The quality of a web site mainly depends on the availability and response time, directly affect the user experience, allowing us to detect it in the end how their company website speed and quality!

Import the pycurl
 Import OS, SYS
 Import Time 
the URL = ' http://www.baidu.com '  # URL detection target 
C = pycurl.Curl () # Create a curl objects 
c.setopt (pycurl.URL, the URL) # define request url constant 
c.setopt (pycurl.CONNECTTIMEOUT, 10) # connection waiting time 
c.setopt (pycurl.TIMEOUT, 10) # timeout 
c.setopt (pycurl.NOPROGRESS, 0) # whether to mask the progress bar 
c.setopt ( pycurl.MAXREDIRS, 5) # Specifies the maximum http redirection number 
c.setopt (pycurl.FORBID_REUSE, 1) # after the completion of interactive forcibly disconnected, do not reuse 
c.setopt (pycurl.DNS_CACHE_TIMEOUT, 60) #Save time DNS information 
# c.setopt (pycurl.URL, 'HTTP: //www.auvgo.com') # Specifies the request of the URL of 
# c.setopt (pycurl.USERAGENT, "Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 2.0.50324 ) ") # configure HTTP request header 

# Create a file object, with open 'wb' mode, for storing the http header and page content 
indexfile = open (os.path.dirname (the os.path.realpath ( __FILE__ )) + " /context.txt " , ' wb ' ) 
c.setopt (pycurl.WRITEHEADER, indexfile) # returns the http header directed to indexfile file object 
c.setopt (pycurl.WRITEDATA , indexfile) # will return http content data file object orientation to indexfile 
the try : 
    c.the perform () # submit a request 
exceptAS E Exception:
     Print ( ' connection error: ' + STR (E)) 
    indexfile.close () 
    c.close () 
    the sys.exit () 
NAMELOOKUP_TIME = c.getinfo (c.NAMELOOKUP_TIME) # Get dns resolution time 
CONNECT_TIME = c .getinfo (c.CONNECT_TIME) # acquisition time to establish a connection 
PRETRANSFER_TIME = c.getinfo (c.PRETRANSFER_TIME) # acquired from the connection time to prepare to transmit consumed 
= c.getinfo (c.STARTTRANSFER_TIME) STARTTRANSFER_TIME # acquired from the connection establishment to transmission time consumed 
TOTAL_TIME = c.getinfo (c.TOTAL_TIME) # acquires the total transmission time 
HTTP_CODE = c.getinfo (c.HTTP_CODE) # obtain HTTP status code
= C.getinfo SIZE_DOWNLOAD (c.SIZE_DOWNLOAD) # acquires download data packet size 
header_size = c.getinfo (c.HEADER_SIZE) # Get HTTP header size 
SPEED_DOWNLOAD = c.getinfo (c.SPEED_DOWNLOAD) # obtains an average download speed 
# printing output data 
Print ( ' the HTTP status code:% S ' % (HTTP_CODE))
 Print ( ' the DNS resolution time:.%. 2F MS ' % (NAMELOOKUP_TIME * 1000 ))
 Print ( ' establish a connection time:.%. 2F MS ' % (* CONNECT_TIME 1000 ))
 Print ( ' ready for transmission time:.%. 2F MS ' % (* PRETRANSFER_TIME 1000 ))
Print ( ' transmission start time:.%. 2F MS ' % (STARTTRANSFER_TIME * 1000 ))
 Print ( ' end of transmission Total time:.%. 2F MS ' % (TOTAL_TIME * 1000 ))
 Print ( ' Download Packet Size:% d bytes / S ' % (SIZE_DOWNLOAD))
 Print ( ' the HTTP header size:% D bytes / S ' % (header_size))
 Print ( ' average download speed:% D bytes / S ' % (SPEED_DOWNLOAD)) 

# close the file and curl Object 
indexfile.close () 
c.close ()

 

Guess you like

Origin www.cnblogs.com/fuyuteng/p/12556338.html