Scene:
One night the company's CDN service node suddenly crash caused some Web pages can not open or open slowly, before testing the CDN node state basically with the change of the host file access-by-time, time-consuming and laborious, I would like to complete the CDN node state test through the script. The main use of xlrd (read tabular data), Httplib (bound CDN node access and State acquisition), Xlsxwriter (write test results). The script reads the data from the formatted table (DATA.XLSX) and outputs it to another table (RESULT.XLSX)
Script written in a casual, various non-standard, followed by follow-up, with code:
1 #-*-coding:utf-8-*-2 Importxlrd3 ImportHttplib4 ImportHashlib5 ImportXlsxwriter6 7 defcdntest (Ip,url,dn,result,ro):8 Try:9Conn=httplib. Httpconnection (IP)#IP AddressTenConn.request (method="GET",#send the request as Get www.ci123.com OneUrl=url,#the requested web path Aheaders={'Host':d N,#host name inside the request header, - "user-agent":"mozilla/5.0 (X11; Linux x86_64) applewebkit/537.36 (khtml, lik e Gecko) chrome/33.0.1750.152 safari/537.36",#Simulation Browser - "Referer":"im is test"})#Request Source theResponse=conn.getresponse ()#get CDN's response content information - PrintI#Output IP - PrintResponse.Status#output HTTP status code - PrintResponse.getheaders ()#Print response header information, expressed as a list of nested tuples + PrintHASHLIB.MD5 (Response.read ()). Hexdigest ()#read the content information of the response, and then calculate the MD5 value -Conn.close ()#Link Close +Result.write (RO, 3, Response.Status) A except: at Print " ====================================" - Print "|| "+ip+"Error | |" - Print " ====================================" -Result.write (RO, 3,"Error") - -Resultbook=xlsxwriter. Workbook ('result.xlsx') inresult=Resultbook.add_worksheet () -Result.write ('A1', u'Domain name') toResult.write ('B1', u'Path') +Result.write ('C1', u'IP') -Result.write ('D1', u'Status') the *Data=xlrd.open_workbook ('data.xlsx') $Table =data.sheets () [0]Panax Notoginsengnrows = Table.nrows#Number of rows -Ncols = Table.ncols#Number of columns thecolnames = table.row_values (0)#a row of data +List =[] A forRowNuminchRange (1, nrows): therow =table.row_values (rownum) + ifrow: -App = {} $ forIinchRange (len (colnames)): $App[colnames[i]] =Row[i] - list.append (APP) - theRo=1 - forRowinchlist:Wuyiurl=row['URL Path'] the #Print URL -ip=row['CDN IP'] Wu #Print IP -dn=row['Domain Name'] About #Print DN $ result.write (RO, 0, DN) -Result.write (RO, 1, IP) -Result.write (RO, 2, URL) - cdntest (Ip,url,dn,result,ro) ARo=ro+1 + resultbook.close () the Print " Done"
Description
Input table Format (partial): data.xlsx:
Domain name is the company top-level domain name, CDN IP is the CDN node Ip,url path is the Web path.
Put the data in the format into the table in the project directory under the name data.xlsx
Run results (partial):
Command line output:
Test demerit automatically output to the table (RESULT.XLSX):
Python Script Test CDN node Status