python写一个网络测速脚本_测试网站页面网速的一个简单Python脚本

无聊之余,下面分享一个Python小脚本:测试网站页面访问速度

[root@huanqiu ~]# vim pywww.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@huanqiu ~]# chmod 755 pywww.py

测试网页,比如www.huanqiu.com

[root@huanqiu ~]# python pywww.py www.huanqiu.com

HTTP响应状态: 200

DNS解析时间:2.56 ms

建立连接时间: 4.92 ms

准备传输时间: 4.93 ms

传输开始时间: 13.08 ms

传输结束时间: 17.71 ms

下载数据包大小: 40101 bytes/s

HTTP头大小: 356 bytes/s

平均下载速度: 2210 k/s

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值