实现代码如下:
#接口调试 import requests,time class api_demo1: def __init__(self,RequestWay,url,data): self.s=requests.session() self.get_login(RequestWay,url,data) #登录(只考虑了get、post请求) def get_login(self,RequestWay,url,data): if RequestWay == 'post': result = self.s.post(url,data) else: result = self.s.get(url) if "登录成功" in result.text: print('登录成功') else: print('登录失败') #普通请求调试 def get_request(self,RequestWay,url,data): if RequestWay == 'post': result= self.s.post(url,data) else: result=self.s.get(url) print(result.text) #上传附件 def get_UploadFile(self,url,ufile): # ufile={'fileToUpload':('11.txt',open(r'C:UsersAdministratorDesktop12.txt','rb'))} r1=self.s.post(url,files=ufile) print(r1.text) #上传附件和请求数据 def get_UploadFiles(self,url,data,ufile): # ufile = [("files[]", ("2.png", open("d:\1.png", "rb"), "image/png")),("files[]", ("2.png", open("d:\2.png", "rb"), "image/png")),] r2= self.s.post(url,data=data,files=ufile) print(r2.text) #文件下载 def download_file(self,url): r2=self.s.get(url) name=time.strftime('%Y%m%d%H%M%S') # 若下载的是xlsx格式,改为"%d.xlsx"%name即可,图片等同样改成相应格式 f=open("%d.pdf"%name, "wb") f.write(r2.content) f.close()