Using Pycurl to monitor HTTP response time script sharing in Python

Source: Internet
Author: User
Tags http post response code
Recently need to monitor the node to the source station itself, a simple ping can detect something, but the HTTP request for the check also to be carried out, so studied the next pycurl.

Pycurl is a Python library implemented in C, although it is said to be not so pythonic, but very efficient, it supports the majority of 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, cookie S, user+password authentication, File transfer resume, HTTP proxy tunneling and more!

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

The following script checks for a given URL, prints out the corresponding code, responds to size, establishes the connection time, prepares the transfer time, transmits the first byte time, and completes the time.

#!/usr/bin/python# coding:utf-8import stringioimport pycurlimport sysimport osclass test:def __init__ (self): s  elf.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 Run 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

Pycurl Some of the response information:
(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. Pretransfer_time the time pycurl after the connection is over to the start of the transfer. Starttransfer_time receives the time Pycurl of the first byte. Total_time the total time of the last request Pycurl. Redirect_time If there is a turn, the time spent is pycurl. Effective_urlpycurl.http_code HTTP Response Code Pycurl. Redirect_count the number of redirects pycurl. Size_upload the size of the uploaded data Pycurl. Size_download the downloaded data size pycurl. Speed_upload upload speed Pycurl. Header_size head size Pycurl. Request_size Request Size Pycurl. Content_length_download download Content length Pycurl. Content_length_upload upload content length Pycurl. Content_Type the type of content Pycurl. Response_code Response Code Pycurl. Speed_download download Speed Pycurl. Ssl_verifyresultpycurl.info_filetime the time information for the file Pycurl. Http_connectcode HTTP Connection Code pycurl. HTTPAUTH_AVAILpycurl.PROXYAUTH_AVAILpycurl.OS_ERRNOpycurl.NUM_CONNECTSpycurl.SSL_ENGINESpycurl.INFO_ COOKIELISTpycurl.LASTSOCKETpycurl.FTP_ENTRY_PATH
  • 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.