HTTP service requires Pycurl module to monitor the service

Source: Internet
Author: User

Recent operation is still relatively idle, write a piece of their own experience. You should have done the HTTP service for the OPS. Like some electronic mall, or some internet companies, Web services, such as is crucial, recently read the Liu Tians eldest brother's book feel that their operation and maintenance platform should also be able to monitor the service, and so on, today learned the Pycurl module, here to record:

Module-related description: c = Pycurl. Curl ()     #创建一个curl对象   c.setopt (pycurl. ConnectTimeout, 5)     #连接的等待时间, set to 0 do not wait   c.setopt (pycurl. TIMEOUT, 5)     #请求超时时间   c.setopt (pycurl. noprogress, 0)     #是否屏蔽下载进度条, not 0 shielded   c.setopt (Pycurl. Maxredirs, 5)     #指定HTTP重定向的最大数   c.setopt (pycurl. Forbid_reuse, 1)     #完成交互后强制断开连接, do not reuse   c.setopt (Pycurl. fresh_connect,1)     #强制获取新的连接, which is the replacement of connections in the cache   c.setopt (pycurl. dns_cache_timeout,60)     #设置保存DNS信息的时间, default is 120 seconds   c.setopt (Pycurl. URL, "http://www.baidu.com")     #指定请求的URL   c.setopt (pycurl. UserAgent, "mozilla/5.2 (compatible; MSIE 6.0; Windows NT 5.1; SV1,. NET CLR 1.1.4322,. NET CLR 2.0.50324)     #配置请求HTTP头的User-agent  c.setopt (pycurl. Headerfunction, GetHeader)   #将返回的HTTP header directed to the callback function getheader  c.setopt (pycurl. Writefunction, GetBody)     #将返回的内容定向到回调函数getbody   C.SEtopt (Pycurl. Writeheader, fileobj)      #将返回的HTTP header directed to Fileobj file object   c.setopt (Pycurl. WriteData, Fileobj)     #将返回的HTML内容定向到fileobj文件对象 c = pycurl. Curl ()     #创建一个curl对象   C.getinfo (pycurl. Http_code)     #返回的HTTP状态码   C.getinfo(Pycurl. Total_time) #传输结束所消耗的总时间 C.getinfo (Pycurl. Namelookup_time) #DNS解析所消耗的时间 C.getinfo (Pycurl. Connect_time) #建立连接所消耗的时间 C.getinfo (Pycurl. Pretransfer_time) #从建立连接到准备传输所消耗的时间 C.getinfo (Pycurl. Starttransfer_time) #从建立连接到传输开始消耗的时间 C.getinfo (Pycurl. Redirect_time) #重定向所消耗的时间 C.getinfo (Pycurl. Size_upload) #上传数据包大小 C.getinfo (Pycurl. Size_download) #下载数据包大小 C.getinfo (Pycurl. Speed_download) #平均下载速度 C.getinfo (Pycurl. Speed_upload) #平均上传速度 C.getinfo (Pycurl. Header_size) #HTTP头部大小JavaScript Tutorialshttp://www.17javascript.com/

All right, let's just write a script to get this job done: Direct code: #!/usr/bin/env python #-*-coding:utf-8-*-import os,sys import time Import sys Import Pycurl

Class Test:     def __init__ (self):         self.contents = '      def callback (Self,curl):         self.contents = self.contents + Curl def test_gzip (URL):     t = Test ()     C = Pycurl. Curl ()       c.setopt (Pycurl. Writefunction,t.callback)     c.setopt (Pycurl. ENCODING, ' gzip ')     c.setopt (Pycurl. Url,input_url)     c.perform ()         namelookup_time =  c.getinfo (c.namelookup_time)       connect_time =  c.getinfo (c.connect_time)     Pretransfer_time =   c.getinfo (c.pretransfer_time)              starttransfer_time = C.getinfo (c.starttransfer_time)        total_time = C.getinfo (c.total_time)     http_code =  C.getinfO (c.http_code)       size_download =  c.getinfo (c.size_download)       Header_size = C.getinfo (c.header_size)        speed_download=c.getinfo (c.speed_download)        print "HTTP Status code:%s"% (http_code)       print "DNS resolution time:%.2f MS"% ( namelookup_time*1000)       print "Establish connection time:%.2f ms"% (connect_time*1000)       Print "Ready to transfer time:%.2f MS"% (pretransfer_time*1000)       print "Transfer start time:%.2f ms"% (starttransfer_time*       print "Total transfer end time:%.2f MS"% (total_time*1000)       print "Download packet size:%d BYTES/S "% (size_download)       print" HTTP header size:%d byte "% (header_size)       print "Average download speed:%d bytes/s"% (speed_download)   if __name__ = = ' __main__ ':     url = sys.argv[1]  &n bsp;  test_gzip (URL)

#备注, the reason for writing a class function is because, pycurl. Writefunction Here we define is to return a function, and then we give an empty function is good, originally tried to generate file results can not be generated.

HTTP service requires Pycurl module to monitor the service

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.