mock接口開發
首先需要安裝? Flask 模塊? :pip install flask
然后引用? ?from flask import request #想獲取到請求參數的話,就得用這個
lanxia =?flask.Flask(__name__) #把這個python文件當做一個web服務
@lanxia.server('/login',[ ' post ' , ' get ' ] )#第一個參數是路徑,第二個參數是請求方式;如果不填寫默認為get方式
def web():#函數和上一行必須連著,不能有空行
? ? username = request.values.get('n')??#get請求獲取值的方式,‘n’代表入參時候的key
????pwd = request.values.get('p')#get請求獲取值的方式,‘p’代表入參時候的key
????json_user_id = request.json.get('a')?#post請求方式(json串)獲取值的方式,‘a’代表入參時候的key
????json_sign = request.json.get('b')#post請求方式(json串)獲取值的方式,‘a’代表入參時候的key
lanxia.run(port=8003,debug=True,host =’127.0.0.1’)#運行這個服務,port端口號(不能被占用),debug=Ture代表開啟每次修改代碼后自動重啟服務,host代表他人如果想要訪問這個接口地址時候的ip
所以值返回來的格式都是字符串類型
?
?flask.request.headers.get('傳的header的key')#接口獲取header的方法
獲取header方法:request.headers
?flask.request.cookies.get('傳的cookie的key')#接口獲取key的方法
獲取cookie方法:request.cookies
?
file = {“ker”:open(‘haha.py’)}#傳的value是文件句柄
?flask.request.files.get('傳的files的key')#接口獲取key的方法
獲取文件方法:自動上傳到了設置好的路徑
?
下載文件
接口代碼
@app.route('/upload',methods=['post'])
?
?
寫入Excel
需要安裝xlwt模塊:pip install xlwt
import xlwt? ?
book ?= xlwt.Workbook()#創建一個excel??
sheet = book.add_sheet('lanxia')#添加一個sheet頁
title = ['姓名','班級','住址','手機號']
data = [
????['','巨蟹座','中南海',110],
????['水瓶座','巨蟹座','紫禁城',119]
]
i=0#控制列
for j in title:
????#j是每次循環title的內容
????sheet.write(0,i,j)#0是行不變,i是列,每次循環的內容
????i+=1#每次循環的時候列都加1
line=1#控制寫的行
for d in data:#外層循環是控制行數的
????row = 0#代表的列,列每次都變
????for dd in d:#控制列的
????????sheet.write(line,row,dd)#行,列,內容
????????row+=1#列每次都要加一
????line+=1
book.save('skkk8.xls')#后綴只能用xls,要不然打不開
?
讀取Excel
需要安裝xlwd模塊:pip install xlrd
book = xlrd.open_workbook('D:\Documents\Tencent Files\837221976\FileRecv\測試用例.xlsx')
sheet = book.sheet_by_name('Sheet1')
rows = sheet.nrows#sheet頁里面的行數
clos = sheet.ncols#sheet頁里面的列數
print(sheet.cell(1,1).value)#通過指定行和列去獲取到單元格里面的內容
row_data = sheet.row_values(1)#獲取第一行的內容
for i in range(rows):
????print(sheet.row_values(i))#獲取第幾行的數據
?
修改excel
?
需要安裝xlutils模塊:pip install?xlutils
from xlutils.copy import copy#拷貝excel模塊方法
import xlrd#修改時需要使用讀
import os
#1、打一要修改的excel
#2、再打開另一個excel
#3、把第一個excel里面修改東西寫到第二個里頭
#4、把原來的excel刪掉,新的excel名改成原來的名字
book = xlrd.open_workbook('stu.xls')
#復制一個excel
new_book = copy(book)#復制了一份原來的excel
#通過獲取到新的excel里面的sheet頁
sheet = new_book.get_sheet(0)#獲取到第一個sheet頁
sheet.write(6, 0, 'Dandan Sun')#寫入excel,第一個值是行,第二個值是列
new_book.save('stu_new.xls')#保存新的excel,保存excel必須使用后綴名是.xls的,不是能是.xlsx的
os.remove('stu.xls')#刪除舊的文檔
os.rename('stu_new.xls','stu.xls')#重命名(“舊名字“,”最新命名的名字”)
?