Using Pycurl to monitor HTTP response time script sharing in Python _python

Source: Internet
Author: User
Tags curl http post response code in python

Recently need to the node to the source station to do their own monitoring, simple ping can detect some things, but the HTTP request check also to carry out, so we studied the next pycurl.

Pycurl is a C-language Python library, although it is said to be not so pythonic, but very efficient, it supports the majority of the 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 is a lot of agreement, I need to be the HTTP one, relative to Urlib, this library may be faster.

The following script checks for a given URL, prints out the HTTP corresponding code, responds to the size, establishes the connection time, prepares the transmission time, transmits the first byte time, completes the 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 (Inpu T_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 ' print '%d%d total_time%f%f%f '% (%f P_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_time<
c2/>200 8703 0.748147 0.748170 1.632642 1.636552

Some response information for 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. Pretransfer_time the time after the connection to the start of the transfer
Pycurl. Starttransfer_time receives the first byte of time
Pycurl. Total_time The total time pycurl the last request
. Redirect_time If there is a shift, the time spent

Pycurl. Effective_url
Pycurl. Http_code HTTP Response Code
pycurl. The number of Redirect_count redirects
pycurl. Size_upload upload the data size
pycurl. Size_download the size of the downloaded data
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_verifyresult
pycurl.info_filetime file's 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 Article

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.