Probing Web service quality using Pycurl

Source: Internet
Author: User

Installation method of 1:pycurl module

Install Pycurl

2: The example code below, is implemented under Python3, if the use of Python2 slightly modified can

#-*-coding:utf-8-*-ImportOs,sysImport TimeImportSYSImportPycurlurl="https://www.baidu.com"C=Pycurl. Curl () c.setopt (Pycurl. URL, url)#Connection Timeout time, 5 secondsC.setopt (Pycurl. ConnectTimeout, 5)#Download Timeout time, 5 secondsC.setopt (Pycurl. TIMEOUT, 5) c.setopt (Pycurl. Forbid_reuse,1) c.setopt (Pycurl. Maxredirs,1) c.setopt (Pycurl. Noprogress,1) c.setopt (Pycurl. Dns_cache_timeout,30) Indexfile= Open (Os.path.dirname (Os.path.realpath (__file__))+"/content.txt","WB") c.setopt (Pycurl. Writeheader, Indexfile) c.setopt (Pycurl. WriteData, Indexfile)Try: C.perform ()exceptException as E:Print("connecion Error:"+str (e)) indexfile.close () C.close () sys.exit () 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("established connection time:%.2f Ms"% (connect_time*1000))Print("Ready for transfer time:%.2f MS"% (pretransfer_time*1000))Print("Transfer start time:%.2f Ms"% (starttransfer_time*1000))Print("total time of transmission end:%.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)) Indexfile.close () c.close ()

Probing Web service quality using Pycurl

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.