标签:
[root@kvmserver-node1 mnt]# cat pycurl.py
#!/usr/bin/python
# coding: UTF-8
import StringIO,pycurl,sys,os,time
class idctest:
def __init__(self):
self.contents = ‘‘
def body_callback(self,buf):
self.contents = self.contents + buf
def test_gzip(input_url):
t = idctest()
#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.setopt(pycurl.MAXREDIRS, 5)
c.perform()
http_code = c.getinfo(pycurl.HTTP_CODE)
dns_resolve = c.getinfo(pycurl.NAMELOOKUP_TIME)
http_conn_time = c.getinfo(pycurl.CONNECT_TIME)
http_pre_trans = c.getinfo(pycurl.PRETRANSFER_TIME)
http_start_trans = c.getinfo(pycurl.STARTTRANSFER_TIME)
http_total_time = c.getinfo(pycurl.TOTAL_TIME)
http_size_download = c.getinfo(pycurl.SIZE_DOWNLOAD)
http_header_size = c.getinfo(pycurl.HEADER_SIZE)
http_speed_downlaod = c.getinfo(pycurl.SPEED_DOWNLOAD)
print ‘HTTP响应状态: %d‘ %http_code
print ‘DNS解析时间:%.2f ms‘ %(dns_resolve*1000)
print ‘建立连接时间: %.2f ms‘ %(http_conn_time*1000)
print ‘准备传输时间: %.2f ms‘ %(http_pre_trans*1000)
print "传输开始时间: %.2f ms" %(http_start_trans*1000)
print "传输结束时间: %.2f ms" %(http_total_time*1000)
print "下载数据包大小: %d bytes/s" %http_size_download
print "HTTP头大小: %d bytes/s" %http_header_size
print "平均下载速度: %d k/s" %(http_speed_downlaod/1024)
if __name__ == ‘__main__‘:
input_url = sys.argv[1]
test_gzip(input_url)
[root@kvmserver-node1 mnt]# chmod 755 pycurl.py
[root@kvmserver-node1 mnt]# python prl.py www.huanqiu.com
HTTP响应状态: 200
DNS解析时间:5514.43 ms
建立连接时间: 5735.63 ms
准备传输时间: 5735.78 ms
传输开始时间: 6558.61 ms
传输结束时间: 6797.67 ms
下载数据包大小: 40732 bytes/s
HTTP头大小: 361 bytes/s
平均下载速度: 5 k/s
[root@kvmserver-node1 mnt]# python prl.py www.baidu.com
HTTP响应状态: 200
DNS解析时间:509.20 ms
建立连接时间: 548.33 ms
准备传输时间: 548.45 ms
传输开始时间: 589.60 ms
传输结束时间: 591.98 ms
下载数据包大小: 27132 bytes/s
HTTP头大小: 1031 bytes/s
平均下载速度: 44 k/s
标签:
原文地址:http://www.cnblogs.com/kevingrace/p/5840951.html