The practice of Python automation module

Source: Internet
Author: User

1. Probing Web Service quality methods:

Python version 3.5.2,pycurl version: pycurl-7.43.0.win-amd64-py3.5

#-*-coding:utf-8-*-import os, sysimport timeimport sysimport pycurlurl = "http://www.baidu.com" #探测的目标URLc = Pycurl. Curl () #创建一个Curl对象c. setopt (Pycurl. URL, url) #定义请求的URL常量c. setopt (Pycurl. ConnectTimeout, 5) #定义请求连接的等待时间c. setopt (Pycurl. TIMEOUT, 5) #定义请求超时时间c. setopt (Pycurl. Forbid_reuse, 1) #屏蔽下载进度条c. setopt (Pycurl. Maxredirs, 1) #完成交互后强制断开连接, do not reuse c.setopt (Pycurl. Noprogress, 1) #指定HTTP重定向的最大数为1c. setopt (Pycurl. Dns_cache_timeout, #设置保存DNS信息的时间为30秒)
#创建一个文件对象, opened as "WB" to store the returned HTTP header and page contents Indexfile = open (Os.path.dirname (Os.path.realpath (__file__)) + "/content.txt "," WB ") c.setopt (Pycurl. Writeheader, Indexfile) #将返回的HTTP header directed to the Indexfile file Object C.setopt (Pycurl. WriteData, Indexfile) #将返回的HTML内容定向到indexfile文件对象try: C.perform () #提交请求except Exception as E:print ("Connecion E Rror: "+ str (e)) Indexfile.close () C.close () sys.exit () Namelookup_time = C.getinfo (c.namelookup_time) #获取 DNS resolution 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) #获取HTTP状态码SIZE_DOW nload = C.getinfo (c.size_download) #获取下载数据包大小HEADER_SIZE = C.getinfo (c.header_size) #获取HTTP头部大小S Peed_download = C.getinfo (c.speed_download) #获取平均下载速度print ("HTTP status code:%s "% (Http_code)) print (" DNS resolution time:%.2f MS "% (Namelookup_time *)) print (" Establish connection time:%.2f ms "% (Connect_time * 1000) Print ("Ready to transfer time:%.2f MS"% (Pretransfer_time *)) print ("Transfer start time:%.2f ms"% (Starttransfer_time *)) print ("Total time to transfer end: %.2f ms "% (Total_time *)) print (" Download packet size:%d bytes/s "% (size_download)) print (" HTTP Header size:%d byte "% (header_size)) Prin T ("Average download speed:%d bytes/s"% (speed_download)) Indexfile.close () C.close ()

Operation Result: HTTP status code: 200

DNS resolution time: 16.00 MS

Established connection time: 47.00 ms

Ready for transfer time: 47.00 MS

Transfer start time: 109.00 ms

Total time of transmission end: 219.00 ms

Download packet size: 102198 bytes/s

HTTP Header size: 989 byte

Average download speed: 466657 bytes/s

2, Pip and easy-install use to download python-related extension resource pack, cmd into the downloaded Pip (pip-9.0.1) and Easy_install (ez_setup-0.9) folder, using Python Setup.exe install installation, the 64-bit operating system easy_install use the Python ez_setup.py install installation, After installation, the corresponding file will be generated in the Scripts folder under the python3.5 (I am using the python3.5.2 version) file, and then it can be used under scripts in cmd, for example, to install the Xlsxwriter module, just execute PIP Install Xlsxwriter or Easy_install Xlsxwriter, as follows:

Easy_install (ez_setup-0.9): https://pypi.python.org/pypi/ez_setup/0.9 pip:https://pypi.python.org/pypi/pip/9.0.1

3. Xlsxwriter Module Function Demonstration

#!/usr/bin/env python3# coding:utf-8import xlsxwriterworkbook = xlsxwriter. Workbook (' demo.xlsx ') #创建一个excel文件worksheet = Workbook.add_worksheet ()  # Creates a sheet object. Worksheet.set_column (' a:a ', #)      # Set the first column a width of 20 pixels bold = Workbook.add_format ()          #定义一个加粗的格式对象bold. Set_bold () worksheet.write (' A1 ', ' Hello ')       #A1单元格写入 "Hello" worksheet.write (' A2 ', ' World ', bold) #在A2单元格写入 "world" and refer to bold Format Object Boldworksheet.write (' B2 ', U ' Chinese test ', bold) #在B2单元格写入 "Chinese test" and refer to bold Format Object Boldworksheet.write (2, 0, +)   #用行列表示法写入数字 ' 32 ' with ' 35.5 ' Worksheet.write (3, 0, 35.5) #行列表示法的单元格下标以0作为起始值, ' 3,0 ' is equivalent to ' A3 ' Worksheet.write (4, 0, ' =sum (A3:A4) ') #求A3: A4 and writes the result to ' 4,0 ', which is ' A5 ' #worksheet. Insert _image (' B5 ', ' Img/python-logo.png ') #  Insert Picture in B5 cell workbook.close ()

Input results such as:

4.

The practice of Python automation module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.