Python self-Study code optimization 1

Source: Internet
Author: User

Optimized the last code, request method can receive get and post two, the code in the two ways to judge.

The test result extracts the name of the test case, the status returned by the request, and the time of the request.

1 #Coding:utf-82 Importxlrd3 ImportRequests4 5 #The global variables that are used in the following6nrows =07Rdict = {}#Store Request Parameters8Relist=[]#Store returned results9 Ten #Import data from Excel One defimptestcase (): A  -FName ='testcase.xlsx' -Sheet_name='case1' theBK =Xlrd.open_workbook (fname) -  -     Try: -Sheet =bk.sheet_by_name (sheet_name) +     except: -         Print("no sheet in%s named Sheet1"%fname) +  A     Globalnrows atnrows =sheet.nrows -Ncols =Sheet.ncols -  -      forIinchRange (0,ncols): -data =sheet.col_values (i) -         Globalrdict inRdict[data[0]]=data[1:] -  to  + #encapsulates the data that is read by Excel into a request and sends -  the defsendpost (): *     #response = requests.post (Url,json = data,headers = Headers,verify=false) $      forIinchRange (0,nrows-1):Panax NotoginsengURL = rdict['URL'][i] -  the         ifrdict['Headers'][i]!="': +headers = eval (rdict['Headers'][i])#str to dict A         Else: theheaders = {} +  -         ifrdict['JSON'][i]!="': $JSON = rdict['JSON'][i]#str to dict $         Else: -JSON = {} -  the         ifrdict['params'][i]!="': -params = eval (rdict['params'][i])#str to dictWuyi         Else: theparams = {} -  Wu         Try: -             ifrdict['Method']=='Get': AboutR = requests.get (url,headers = Headers,params = params,verify=False) $             Else: -R = requests.post (Url,json = Json,headers = headers,verify=False) - r.raise_for_status () -r.encoding=r.apparent_encoding A             Globalrelist +Relist.append ('Casename:'+rdict['Casename'][i]) theRelist.append ('Status:'+str (r.status_code)) -Relist.append ('Date:'+r.headers['Date']) $              the         exceptException as E: the             Print(e) the             Print('fail') the relist.append (str (e)) -  in #output The returned results to HTML to form an HTML report (simple display of knowledge, no style) the defoutput_html (): theFout=open ('output.html','W', encoding='UTF-8', newline="')  AboutFout.write ('')  theFout.write ('')  theFout.write ('<meta charset = "UTF-8"/>')  theFout.write ('')  +Fout.write ('<body>') -Fout.write ('') theFout.write ('<table>')BayiFout.write ('<tr>') the     #fout.write (' <td>%s</td> '% relist) the      forIinchRange (0,len (relist)): -Fout.write ('<td>%s</td>'%Relist[i]) -Fout.write ('</tr>') theFout.write ('</table>') theFout.write ('</body>') theFout.write ('')  the fout.close () -  the if __name__=='__main__': the imptestcase () the sendpost ()94Output_html ()

Test Cases

Test results

After the plan:

1. Optimizing Import Use cases and encapsulating code for requests

2. Optimize test results and display more user-friendly pages

3. Since this short learning process uses Notepad to write code, as the amount of code increases, the next step is to consider using Ides such as pycharm for ease of administration.

Off Topic:

Just started to learn python, the code is too low, just to realize the function, blog Park, the big God do not despise, hope to receive good advice.

Python self-Study code optimization 1

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.