pycurl 是一个用C语言写的libcurl Python实现,速度很快,比urllib和httplib都快功能非常强大,支持的操作协议有FTP、HTTP、HTTPS、TELNET等,可以理解成Linux下curl命令功能的Python封装,简单易用.在最近参与的自动化运维平台上使用它检测web应用节点的性能参数,利用libcurl包提供的这些常量值来达到探测Web服务质量的目的.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 c = pycurl.Curl() c.setopt(pycurl.CONNECTTIMEOUT, 5 ) c.setopt(pycurl.TIMEOUT, 5 ) c.setopt(pycurl.NOPROGRESS, 0 ) c.setopt(pycurl.MAXREDIRS, 5 ) c.setopt(pycurl.FORBID_REUSE, 1 ) c.setopt(pycurl.FRESH_CONNECT,1 ) c.setopt(pycurl.DNS_CACHE_TIMEOUT,60 ) c.setopt(pycurl.URL,"http://www.baidu.com" ) 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)" ) c.setopt(pycurl.HEADERFUNCTION, getheader) c.setopt(pycurl.WRITEFUNCTION, getbody) c.setopt(pycurl.WRITEHEADER, fileobj) c.setopt(pycurl.WRITEDATA, fileobj) getinfo(option) c = pycurl.Curl() c.getinfo(pycurl.HTTP_CODE) c.getinfo(pycurl.TOTAL_TIME) c.getinfo(pycurl.NAMELOOKUP_TIME) 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)
脚本如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 import osimport sysimport timeimport pycurl''' 探测web服务质量 ''' URL = 'http://www.XXX.com' c = pycurl.Curl() c.setopt(pycurl.URL,URL) c.setopt(pycurl.CONNECTTIMEOUT,10 ) c.setopt(pycurl.TIMEOUT,20 ) c.setopt(pycurl.NOPROGRESS,1 ) c.setopt(pycurl.FORBID_REUSE,1 ) c.setopt(pycurl.MAXREDIRS,1 ) c.setopt(pycurl.DNS_CACHE_TIMEOUT,30 ) c.setopt(c.USERAGENT, 'Foo' ) indexfile = open (os.path.dirname(os.path.realpath(__file__))+'/content' ,'wb' ) c.setopt(pycurl.WRITEHEADER,indexfile) c.setopt(pycurl.WRITEDATA,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) 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状态码: %s' %(HTTP_CODE))print ('DNS解析状态:%.2f ms' %(NAMELOOKUP_TIME*1000 ))print ('建立连接时间: %.2f ms' %(CONNECT_TIME*1000 ))print ('准备传输时间:%.2f ms' %(PRETRANSFER_TIME*1000 ))print ('传输开始时间:%.2f ms' %(STARTTRANSFER_TIME*1000 ))print ('传输结束总时间:%.2f ms' %(TOTAL_TIME*1000 ))print ('下载数据包大小:%d bytes/s' %(SIZE_DOWNLOAD))print ('HTTP 头部大小:%d bytes/s' %(HEADER_SIZE))print ('平均下载速度:%d bytes/s' %(SPEED_DOWNLOAD))indexfile.close() c.close()
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 HTTP/1.1 200 OK Date: Thu, 19 Nov 2015 10 :28 :24 GMT Server: Apache/2.4 .3 (Unix) OpenSSL/1.0 .1 e-fips X-Powered-By: PHP/5.4 .26 X-Pingback: http://www.tianfeiyu.com/xmlrpc.php Transfer-Encoding: chunked Content-Type : text/html; charset=UTF-8 ... ...
参考文章: