zoukankan      html  css  js  c++  java
  • Python3.7应用实验(2)

    一:文件和目录操作

    1:文件操作

    >>> fp=open('d:/aaa.txt','w')

    >>> fp.close()

    2:目录操作

    1)获取当前路径

    >>> import os

    >>> print (os.getcwd())

    C:Python37

    (2)创建目录

    >>> import os

    >>> print (os.mkdir('d:/test'))

    (3)判断目录是否存在

    >>> import os

    >>> print (os.path.isdir('d:/test'))

    True

    (4)判断是否为文件

    >>> import os

    >>> print (os.path.isfile('d:/test'))

    二:案例--编写脚本,采集web质量数据到excel

    1:部署web平台(centos

    注意关闭firewalldselinux

    2:用pip安装模块

    PycurlXlsxWriter

    3:编写脚本

    #__.__coding:utf-8 __.__

    import os,sys

    import pycurl

    import xlsxwriter

    URL="http://192.168.10.105"

    c=pycurl.Curl()

    c.setopt(pycurl.URL,URL)

    c.setopt(pycurl.CONNECTTIMEOUT,10)

    c.setopt(pycurl.TIMEOUT,10)

    c.setopt(pycurl.NOPROGRESS,1)

    c.setopt(pycurl.FORBID_REUSE,1)

    c.setopt(pycurl.MAXREDIRS,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)

    c.perform()

    NAMELOOKUP_TIME=c.getinfo(c.NAMELOOKUP_TIME)

    CONNECT_TIME=c.getinfo(c.CONNECT_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 (u"HTTP状态码:%s" %(HTTP_CODE))

    print (u"DNS解析时间:%.2f ms" %(NAMELOOKUP_TIME*1000))

    print (u"建立连接时间:%.2f ms"%(CONNECT_TIME*1000))

    print (u"传输总时间:%.2f ms" %(TOTAL_TIME*1000))

    print (u"下载数据包大小:%d bytes/s" %(SIZE_DOWNLOAD))

    print (u"HTTP头部大小:%d bytes/s" %(HEADER_SIZE))

    print (u"平均下载速度:%d bytes/s" %(SPEED_DOWNLOAD))

    indexfile.close()

    c.close()

    f=open('chart.txt','a')

    f.write(str(HTTP_CODE)+','+str(NAMELOOKUP_TIME*1000)+','+str(CONNECT_TIME*1000)+','+str(TOTAL_TIME*1000)+','+str(SIZE_DOWNLOAD/1024)+','+str(HEADER_SIZE)+','+str(SPEED_DOWNLOAD/1024)+' ')

    f.close()

    workbook=xlsxwriter.Workbook('chart.xlsx')

    worksheet=workbook.add_worksheet()

    chart=workbook.add_chart({'type':'column'})

    title=[URL,u'HTTP状态码',u'DNS解析时间',u'建立连接时间',u'传输结束时间',u'下载数据包时间',u'HTTP头部大小',u'平均下载速度']

    format=workbook.add_format()

    format.set_border(1)

    format_title=workbook.add_format()

    format_title.set_border(1)

    format_title.set_bg_color('#00ff00')

    format_title.set_align('center')

    format_title.set_bold()

    worksheet.write_row(0,0,title,format_title)

    f=open('chart.txt','r')

    line=1

    for i in f:

        head=[line]

        lineList=i.split(',')

        lineList=list(map(lambda i2:int(float(i2.replace(" ",''))),lineList))

        lineList=head+lineList

        worksheet.write_row(line,0,lineList,format)

        line+=1

    average=[u'平均值','=AVERAGE(B2:B)'+str((line-1))+')','=AVERAGE(C2:C)'+str((line-1))+')','=AVERAGE(D2:D)'+str((line-1))+')','=AVERAGE(E2:E)'+str((line-1))+')','=AVERAGE(F2:F)'+str((line-1))+')','=AVERAGE(G2:G)'+str((line-1))+')','=AVERAGE(H2:H)'+str((line-1))+')']

    worksheet.write_row(line,0,average,format)

    f.close()

    def chart_series(cur_row,line):

        chart.add_series({

            'categories': '=Sheet1!$B$1:$H$1',

            'values': '=Sheet1!$B$'+cur_row+':$H$'+cur_row,

            'line': {'color':'black'},

            'name': '=Sheet1!$A'+cur_row,

            })

    for row in range(2,line+1):

        chart_series(str(row),line)

    chart.set_size({'width':876,'height':287})

    worksheet.insert_chart(line+2,0,chart)

    workbook.close()

  • 相关阅读:
    java中BigDecimal的四舍五入小记
    thinking in java学习笔记:14章 类型信息
    java中==和equals
    java中值传递和引用传递
    java中的赋值
    hessian学习笔记
    springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题
    要是满足下面的其中3种情况,你就跑路吧
    vim程序编辑器---常用操作整理
    报错end Kernel panic
  • 原文地址:https://www.cnblogs.com/ccshi/p/13162129.html
Copyright © 2011-2022 走看看