Python uses pycurl to monitor http Response Time script sharing and pythonpycurl

Source: Internet
Author: User

Python uses pycurl to monitor http Response Time script sharing and pythonpycurl

Recently, we need to monitor the node to the source station. Simple ping can detect some things, but http request check is also required. So we studied pycurl.

Pycurl is a python library implemented in C language. Although it is said that it is not so pythonic, It is very efficient and supports many protocols:

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!

There are already a lot of these protocols, and I need an http protocol. This library may be faster than urlib.

The following script checks a given url, prints the http code, response size, connection time, preparation time, transmission time, first byte time, and completion time.

#!/usr/bin/python# coding: UTF-8import StringIOimport pycurlimport sysimport osclass Test:    def __init__(self):        self.contents = ''    def body_callback(self,buf):        self.contents = self.contents + bufdef 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_time200 8703 0.748147 0.748170 1.632642 1.636552

Some response information of pycurl:
(Reference: 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. the time from PRETRANSFER_TIME connection to start transmission pycurl. STARTTRANSFER_TIME receives the time of the first byte pycurl. total time of the request on TOTAL_TIME pycurl. REDIRECT_TIME if there is a redirection, it takes time to pycurl. EFFECTIVE_URLpycurl.HTTP_CODE HTTP response code pycurl. REDIRECT_COUNT redirect times pycurl. the size of the data uploaded by SIZE_UPLOAD is pycurl. SIZE_DOWNLOAD the data size pycurl. SPEED_UPLOAD upload speed: pycurl. HEADER_SIZE header size pycurl. request size: pycurl. CONTENT_LENGTH_DOWNLOAD download content length: pycurl. CONTENT_LENGTH_UPLOAD the length of the uploaded content pycurl. the CONTENT_TYPE content type pycurl. RESPONSE_CODE response code pycurl. SPEED_DOWNLOAD download speed: pycurl. the time information of the SSL_VERIFYRESULTpycurl.INFO_FILETIME file pycurl. HTTP_CONNECTCODE HTTP connection code pycurl. bytes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.