Python USES pycurl to monitor HTTP response time script sharing

  • 2020-04-02 14:34:08
  • OfStack

Recently, I needed to monitor the node to the source station myself. A simple ping can detect something, but the HTTP request also needs to be checked, so I studied pycurl.

Pycurl is a library of python implemented in c. It is said to be not quite pythonic, but it is efficient.


supporting FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. libcurl supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, kerberos, HTTP form based upload, proxies, cookies, user+password authentication, file transfer resume, http proxy tunneling and more!

This pile of protocols is already a lot, I need just one HTTP, compared to urlib, this library may be faster.

The following script checks a given url and prints out the HTTP code, response size, connection setup time, prepare transfer time, first byte transfer time, and completion time.


#!/usr/bin/python
# coding: UTF-8
import StringIO
import pycurl
import sys
import os
class Test:
    def __init__(self):
        self.contents = ''
    def body_callback(self,buf):
        self.contents = self.contents + buf
def test_gzip(input_url):
    t = Test()
    #gzip_test = file("gzip_test.txt", 'w')
    c = pycurl.Curl()
    c.setopt(pycurl.WRITEFUNCTION,t.body_callback)
    c.setopt(pycurl.ENCODING, 'gzip')
    c.setopt(pycurl.URL,input_url)
    c.perform()
    http_code = c.getinfo(pycurl.HTTP_CODE)
    http_conn_time = c.getinfo(pycurl.CONNECT_TIME)
    http_pre_tran = c.getinfo(pycurl.PRETRANSFER_TIME)
    http_start_tran = c.getinfo(pycurl.STARTTRANSFER_TIME)
    http_total_time = c.getinfo(pycurl.TOTAL_TIME)
    http_size = c.getinfo(pycurl.SIZE_DOWNLOAD)
    print 'http_code http_size conn_time pre_tran start_tran total_time'
    print "%d %d %f %f %f %f"%(http_code,http_size,http_conn_time,http_pre_tran,http_start_tran,http_total_time)
if __name__ == '__main__':
    input_url = sys.argv[1]
    test_gzip(input_url)

Script running effect


xu:~/curl$ python pycurl_test.py http://daxuxu.info/
http_code http_size conn_time pre_tran start_tran total_time
200 8703 0.748147 0.748170 1.632642 1.636552

Some response information about pycurl:
(reference: (the link: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html))


pycurl.NAMELOOKUP_TIME  Domain name resolution time 
pycurl.CONNECT_TIME  Remote server connection time 
pycurl.PRETRANSFER_TIME  The time between the last connection and the start of transmission 
pycurl.STARTTRANSFER_TIME  The time to receive the first byte 
pycurl.TOTAL_TIME  Total time requested last time 
pycurl.REDIRECT_TIME  If there is a steering, the time it takes 

pycurl.EFFECTIVE_URL
pycurl.HTTP_CODE HTTP  The response code 
pycurl.REDIRECT_COUNT  Number of redirects 
pycurl.SIZE_UPLOAD  The size of the uploaded data 
pycurl.SIZE_DOWNLOAD  The size of the downloaded data 
pycurl.SPEED_UPLOAD  The upload speed 
pycurl.HEADER_SIZE  Head size 
pycurl.REQUEST_SIZE  The request size 
pycurl.CONTENT_LENGTH_DOWNLOAD  Download content length 
pycurl.CONTENT_LENGTH_UPLOAD  Upload content length 
pycurl.CONTENT_TYPE  Type of content 
pycurl.RESPONSE_CODE  The response code 
pycurl.SPEED_DOWNLOAD  Download speed 
pycurl.SSL_VERIFYRESULT
pycurl.INFO_FILETIME  File time information 

pycurl.HTTP_CONNECTCODE HTTP  Connection code 
pycurl.HTTPAUTH_AVAIL
pycurl.PROXYAUTH_AVAIL
pycurl.OS_ERRNO
pycurl.NUM_CONNECTS
pycurl.SSL_ENGINES
pycurl.INFO_COOKIELIST
pycurl.LASTSOCKET
pycurl.FTP_ENTRY_PATH


Related articles: