Python 3 implementation probe Web service quality

HTTP service is one of the most popular Internet applications. The quality of service is related to the user experience and the operation service level of the website. There are two most commonly used standards: one is the availability of service, such as whether it is in the normal service state, rather than the 404 page not found or 500 page error; the other is the response speed of service, such as the static file download time They are all controlled in milliseconds, and the dynamic CGI is in seconds. In this example, pycurl's setopt and getinfo methods are used to detect the HTTP service quality and obtain the HTTP status code returned by the monitoring URL. The HTTP status code is obtained by pycurl. HTTP code constant and the response time of each link from HTTP request to download completion. It is realized by pycurl. Namelookup? Time, pycurl. Connect? Time, pycurl. Pretransfer? Time, pycurl. R and other constants . In addition, the HTTP response header and page content of the target URL are obtained through pycurl.WRITEHEADER and pycurl.WRITEDATA constants. The implementation source code is as follows:

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

try:
    URL=sys.argv[1]    # Target URL for probe
except Exception as e:
    print ("Error:"+str(e))
    print ("Usage: Please enter the web address")
    sys.exit()
#URL="http://www.baidu.com "ා target URL of probe
c = pycurl.Curl()    #Create a Curl object
c.setopt(pycurl.URL, URL)    #Define the URL constant of the request
c.setopt(pycurl.CONNECTTIMEOUT, 5)    #Define the waiting time for the request connection
c.setopt(pycurl.TIMEOUT, 5)    #Define request timeout
c.setopt(pycurl.NOPROGRESS, 1)    #Block download progress bar
c.setopt(pycurl.FORBID_REUSE, 1)    #Force disconnect after interaction, no reuse
c.setopt(pycurl.MAXREDIRS, 1)    #Specifies that the maximum number of HTTP redirects is 1
c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)    #Set the time to save DNS information to 30 seconds
#Create a file object and open it in "wb" mode to store the returned http header and page content
indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/content.txt", "wb")
c.setopt(pycurl.WRITEHEADER, indexfile)    #Direct the returned HTTP HEADER to the indexfile object
c.setopt(pycurl.WRITEDATA, indexfile)    #Direct the returned HTML content to the indexfile object
try:
    c.perform()    #Submit request
except Exception as e:
    print("connecion error:"+str(e))
    indexfile.close()
    c.close()
    sys.exit()

NAMELOOKUP_TIME =  c.getinfo(c.NAMELOOKUP_TIME)    #Get DNS resolution time
CONNECT_TIME =  c.getinfo(c.CONNECT_TIME)    #Get connection time
PRETRANSFER_TIME =   c.getinfo(c.PRETRANSFER_TIME)    #Get the cancellation from establishing connection to preparing for transmission
                                                      #Time consuming
STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)    #Get cancel from connection establishment to transmission
                                                        #Time consuming
TOTAL_TIME = c.getinfo(c.TOTAL_TIME)    #Total time to get transfer
HTTP_CODE =  c.getinfo(c.HTTP_CODE)    #Get HTTP status code
SIZE_DOWNLOAD =  c.getinfo(c.SIZE_DOWNLOAD)    #Get download packet size
HEADER_SIZE = c.getinfo(c.HEADER_SIZE)    #Get HTTP header size
SPEED_DOWNLOAD=c.getinfo(c.SPEED_DOWNLOAD)    #Get average download speed
#Printout related data
print("HTTP Status code:{}" .format(HTTP_CODE))
print("HTTP Status code:%s" %(HTTP_CODE))
print("DNS Resolution time:%.2f ms"%(NAMELOOKUP_TIME*1000))
print("Connection established:%.2f ms" %(CONNECT_TIME*1000))
print("Ready for transfer time:%.2f ms" %(PRETRANSFER_TIME*1000))
print("Transmission start time:%.2f ms" %(STARTTRANSFER_TIME*1000))
print("Total transfer end time:%.2f ms" %(TOTAL_TIME*1000))
print("Download packet size:%d bytes/s" %(SIZE_DOWNLOAD))
print("HTTP Head size:%d byte" %(HEADER_SIZE))
print("Average download speed:%d bytes/s" %(SPEED_DOWNLOAD))
#Close files and Curl objects
indexfile.close()
c.close()
[root@ opt]# python web.py https://www.baidu.com/
HTTP status code: 200
 HTTP status code: 200
 DNS resolution time: 4.19 ms
 Connection time: 32.45 ms
 Ready to transmit time: 105.58 ms
 Transmission start time: 138.04 ms
 Total transmission end time: 138.11 ms
 Download packet size: 227 bytes/s
 HTTP header size: 690 byte s
 Average download speed: 1644 bytes/s

Tags: pycurl DNS curl Python

Posted on Tue, 31 Dec 2019 10:06:41 -0500 by dkjariwala