python自學1——代碼最佳化1

來源:互聯網
上載者:User

標籤:代碼   requests   excel   append   import   row   for   開始   管理   

最佳化了上次的代碼,請求方式可接收get和post兩種,代碼中對兩種方式做了判斷。

測試結果中提取了測試案例的名稱、請求返回的狀態、請求的時間。

 1 #coding:utf-8 2 import xlrd 3 import requests 4  5 #下文中將用到的全域變數 6 nrows = 0 7 rdict = {} #儲存請求參數 8 relist=[]  #儲存返回結果 9 10 #從excel中匯入資料11 def imptestcase():12 13     fname = ‘testcase.xlsx‘14     sheet_name=‘case1‘15     bk = xlrd.open_workbook(fname)16 17     try:18         sheet = bk.sheet_by_name(sheet_name)19     except:20         print("no sheet in %s named sheet1" %fname)21 22     global nrows23     nrows = sheet.nrows24     ncols = sheet.ncols25 26     for i in range(0,ncols): 27         data = sheet.col_values(i) 28         global rdict29         rdict[data[0]]=data[1:]30 31 32 #將excel讀取的資料封裝成請求,並發送33 34 def sendpost():35     #response = requests.post(url,json = data,headers = headers,verify=False) 36     for i in range(0,nrows-1):37         url = rdict[‘url‘][i]    38 39         if rdict[‘headers‘][i]!=‘‘:40             headers = eval(rdict[‘headers‘][i]) #str to dict41         else:42             headers = {}43 44         if rdict[‘json‘][i]!=‘‘:45             json = rdict[‘json‘][i] #str to dict46         else:47             json = {}48 49         if rdict[‘params‘][i]!=‘‘:50             params = eval(rdict[‘params‘][i]) #str to dict51         else:52             params = {}53 54         try:55             if rdict[‘method‘]==‘get‘:56                 r = requests.get(url,headers = headers,params = params,verify=False)57             else:58                 r = requests.post(url,json = json,headers = headers,verify=False)59             r.raise_for_status()60             r.encoding=r.apparent_encoding61             global relist62             relist.append(‘Casename: ‘+rdict[‘casename‘][i])63             relist.append(‘Status: ‘+str(r.status_code))64             relist.append(‘Date: ‘+r.headers[‘Date‘])65             66         except Exception as e:67             print(e)    68             print(‘fail‘)69             relist.append(str(e))70 71 #把返回的結果輸出到html中,形成HTML報告(知識簡單的展示,沒有樣式)72 def output_html(): 73     fout=open(‘output.html‘,‘w‘,encoding=‘UTF-8‘, newline=‘‘) 74     fout.write(‘<html>‘) 75     fout.write(‘<head>‘) 76     fout.write(‘<meta charset = "UTF-8"/>‘) 77     fout.write(‘</head>‘) 78     fout.write(‘<body>‘)79     fout.write(‘<h1>report<h1>‘)80     fout.write(‘<table>‘)81     fout.write(‘<tr>‘)82     #fout.write(‘<td>%s</td>‘ % relist)83     for i in range(0,len(relist)):84         fout.write(‘<td>%s</td>‘ % relist[i])85     fout.write(‘</tr>‘)86     fout.write(‘</table>‘)87     fout.write(‘</body>‘)88     fout.write(‘</html>‘) 89     fout.close()90 91 if __name__ == ‘__main__‘:92     imptestcase()93     sendpost()94     output_html()

測試案例

測試結果

 

之後的計劃:

1. 最佳化匯入用例以及封裝請求的代碼

2. 最佳化測試結果,展示位更友好的頁面

3. 由於這短時間學習的過程中使用的都是notepad來編寫代碼,隨著代碼量的增加,下一步考慮使用pycharm等IDE,便於管理。

 

題外話:

剛開始學習Python,代碼寫的太low,僅僅為了實現功能而已,部落格園的各位大神請勿鄙視,希望能收到好的建議。

 

python自學1——代碼最佳化1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.