Python Learning Notes-implementing probing Web service quality

Source: Internet
Author: User

Pycurl is a C language written Libcurl Python implementation, the function is very powerful, support operation protocol after FTP, HTTP, HTTPS, Telnet, etc., can be understood as the Linux under the Curl command function Python package, easy to use

      This example uses the method provided by Pycurl to detect the quality of Web services, such as responding to HTTP status code, request delay, HTTP header information, download speed, etc., using this information can be used to locate the service response slow specific links.

Pycurl. The curl () class implements a curl handle object that creates a Libcurl package with no arguments.

The close () method, corresponding to the Curl_easy_cleanup method in the Libcurl package, has no parameters to implement the close, Recycle Curl object.

  The    perform () method, which corresponds to the Curl_easy_perform method in the Libcurl package, has no parameters to implement the request for the Curl object submission.

Setopt (Option,value) method, corresponding to the Curl_easy_setopt method in the Libcurl package, the parameter option is specified by a constant of libcurl, and the value of the parameter value depends on option, which can be a string, integer, Long integer, file object, list or function, etc.

Installing the Pycurl module

[Email protected] ~]# python3-m easy_install-i http://pypi.douban.com/simple/pycurl

Error:

Setuptools.sandbox.UnpickleableException:ConfigurationError ("Could not run Curl-config: [Errno 2] No such file or direct Ory: ' Curl-config ' ",)


First install the Libcurl-devel solution:

[[email protected] ~]# yum -y install  libcurl-devel[[email protected] ~]# python3 -m easy_install -i http:// Pypi.douban.com/simple/ pycurlfinished processing dependencies for pycurl 
#!/usr/bin/python3# _*_ coding:utf-8 _*_import sys,osimport timeimport pycurlurl  =  "http://fm.mykurol.com"     #探测的目标URLc  = pycurl. Curl ()     #创建一个Curl对象c. setopt (Pycurl. Url,url)      #定义请求的URL常量c. setopt (Pycurl. connecttimeout,5)     #定义请求连接的等待时间c. setopt (Pycurl. timeout,5)        #定义请求超时时间c. setopt (Pycurl. noprogress,1)         #屏蔽下载进度条c. setopt (Pycurl. forbid_reuse,1)       #完成交互后强制断开连接, do not reuse c.setopt (Pycurl. maxredirs,1)          #指定HTTP重定向的最大数为1c. setopt (Pycurl. dns_cache_timeout,30)         #设置保存DNS信息的时间为30秒 # Create a File object that opens in Web mode, Used to store returned HTTP headers and page contents Indexfile = open (Os.path.dirname (Os.path.realpath (__file__)) + "/content.txt", "WB") C.setopt (Pycurl. Writeheader, indexfile)       #将返回的HTTP  header directed to IndexfIle file C.setopt (Pycurl. Writedata, indexfile)         #将返回的HTML内容定向到indexfile文件对象try:     c.perform () except exception as e:    print  (" Connection error: "+str (e))     indexfile.close ()     c.close ()     sys.exit () namelookup_time = c.getinfo (c.namelookup_time)   # Get DNS resolution 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)         #获取HTTP状态码SIZE_DOWNLOAD  = c.getinfo (c.size_download)         #获取下载数据包的Size Header_size = c.getinfo (c.header_size)        #获取HTTP头部大小SPEED_ Download = c.getinfo (c.speed_download)      #获取平均下载速度 # Print output related data 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*1000)) print   ("Total transfer end time:%.2f ms"  %  (total_time*1000)) print  ("Download packet size:%d bytes/s"  %  (Size_download)) print  ("HTTP Header size:%d bytes/s"  %  (header_size)) print  ("Average download speed:%d bytes/s"  %   (speed_download)) #关闭文件及curl对象indexfile. Close () C.close ()


Execution Result:

HTTP status code: 200DNS parsing time: 17.44 ms setting up connection time: 17.88 ms Preparing transfer time: 17.89 MS Transfer start time: 39.79 MS Transfer end total time: 39.88 ms download Packet size: 2526 bytes/ Shttp head Size: 389 bytes/s average download speed: 63333 bytes/s

view get HTTP file header and page content Content.txt

http/1.1 200 okdate: fri, 09 jun 2017 03:01:46 gmtserver: apache/ 2.2.15  (CentOS) x-powered-by: php/5.3.3set-cookie: phpsessid=qmhmq2hkbb3v5hs67rf38c5006;  path=/expires: thu, 19 nov 1981 08:52:00 gmtcache-control: no-store,  no-cache, must-revalidate, post-check=0, pre-check=0pragma: no-cachecontent-length:  2526connection: closecontent-type: text/html; charset=utf-8  <!doctype html ><link href= "Css.css"  rel= "stylesheet"  type= "Text/css"  /><title> Mykurol Movie Referral Network </title><link rel= "icon"  href= "Image/logo.ico"  type= "Img/x-ico"  /> <body><link rel= "icon"  href= "Image/logo.ico"  type= "Img/x-ico"  /><div  Class= "in" >    <div class= "header" >         <div class= "MYkurol ">            <a href=" index.php "  class= "Biaoyu" ><strong> this site by Shei design </strong></a>         </div>        <div class= "Key-sousuo" >             <input type= "text"  name= " Mo_key " placeholder=" film/director/actor >             <input type= "Submit"  name= "Mo_sub"  value= "Search" >         </div>        <div class= "Inlogin" >             <a href= "MovEncy.php"  class= " Movie-ency "> Movie Daquan </a>            <a  href= "#"  class= "Movie-guess "> Guess </a>            <a  href= "#"  class= "Movie-album" > Movie albums </a>             <a href= "login.php"  class= "login-sub" > Login </a><a href= " reg.php " class=" reg-sub "> Registration </a>        </div>     </div></div><div style= "z-index:999; position:absolute;  right: 20px; bottom:40% "><div>        </div>    <div style= "padding:0; margin:0;  background-color: #FFF;  width:120px; height:30px ">    <a style=" Color: #666; font-size:12px; " > Links: <a href= "http://www.mygdmec.cn"  style= "text-decoration: None; color: #F9F;  font-size:12px "> Shopping Network </a>    </div></div >        <div class= "Body" >             <div class= "Flo" >                 <div class= "Flo-biaoti" >                      <p href= "#"  class= "Movie-name"  data-toggle= "tooltip"  title= "" Biochemistry Crisis 6 "details" >< Strong> Biochemical Crisis 6</strong></p>                     <p class= "Movie-jieshao" >                           in Washington, D.C., Alice was betrayed by Willie.Class almost loses the last hope. As the only survivor and the last line of defense against the zombie army, Alice must return to the place where the nightmare began-raccoon bear. There the umbrella company is assembling all the forces in an attempt to make a final blow to the remaining survivors. <br>                          Director: Paul Anderson <br>                          Starring: Mira · Jovovich  , Ian Grey, Ali Larter, Rouby Ross, Lee Junki Base, Shawn Roberts, William Livi, Ian Marken  <br>                          Action   /  thriller  /  sci-fi                      </p>                 </div>              </div>  &nbSp;      </div>        <div  class= "Foot" >         </div>     </div></body>


This article is from the "Shei" blog, make sure to keep this source http://kurolz.blog.51cto.com/11433546/1935054

Python Learning Notes-implementing probing Web service quality

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.