#!/usr/bin/env python
# coding:utf-8
import psutil
import time
import sys
from optparse import OptionParser
from datetime import date,datetime
import xlwt
import xlsxwriter
f = open("D:/1.txt", 'w+')
parser = OptionParser()
parser.add_option("-t", "--time", dest="time",
                  help="此参数可查看当前下载占的带宽,-t是测试时间", metavar="10")
parser.add_option("-d", "--deamon", action="store_false", dest="deamon", default=True,
                  help="后台运行此脚本")


def Sysinfo():
    Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time()))
    type(Boot_Start)
    time.sleep(0.5)
    Cpu_usage = psutil.cpu_percent()
    RAM = int(psutil.virtual_memory().total / (1027 * 1024))
    RAM_percent = psutil.virtual_memory().percent
    Swap = int(psutil.swap_memory().total / (1027 * 1024))
    Swap_percent = psutil.swap_memory().percent
    Net_sent = psutil.net_io_counters().bytes_sent
    Net_recv = psutil.net_io_counters().bytes_recv
    Net_spkg = psutil.net_io_counters().packets_sent
    Net_rpkg = psutil.net_io_counters().packets_recv
    BFH = r'%'

    print >> f, " 开机时间:%s" % Boot_Start
    print >> f," CPU使用率:%s%s" % (Cpu_usage, BFH)
    print >> f,"内存:%dM\t内存使用率:%s%s" % (RAM, RAM_percent, BFH)
    print >> f,"交换分区内存:%dM\t交换分区使用率:%s%s" % (Swap, Swap_percent, BFH)
    print >> f," 发送:%d Byte\t发送包数:%d个" % (Net_sent, Net_spkg)
    print >>f," 接收:%d Byte\t接收包数:%d个" % (Net_recv, Net_rpkg)

    for i in psutil.disk_partitions():
        print >> f," 盘符: %s 挂载点: %s 磁盘使用率: %s%s" % (i[0], i[1], psutil.disk_usage(i[1])[3], BFH)


def Net_io(s):
    x = 0
    sum = 0
    while True:
        if x >= s:
            break
        r1 = psutil.net_io_counters().bytes_recv
        time.sleep(1)
        r2 = psutil.net_io_counters().bytes_recv
        y = r2 - r1
        print "%.2f Kb/s" % (y / 1024.0)
        sum += y
        x += 1
    result = sum / x
    print >> f,"1;33m%s秒内平均速度:%.2f Kb/s " % (x, result  /1024.0)


if __name__ == "__main__":
    (options, args) = parser.parse_args()
    if options.time:
        Net_io(int(options.time))
    else:
        Sysinfo()

最后结果

wKiom1kz3snRqVYNAAC2kxZJdOM110.png-wh_50


由于才学,大神请多指导,谢谢,本来想直接想把结果插入excel中,目前还在研究,如有大神,请不吝赐教,感谢。