1.安装requests库
输入 python -m pip install requests
2.简单使用-post
import requests import json data = { 'username':'dajiu', 'password':'123456' } #以字典方式传入数据 res = requests.post(url='http://127.0.0.1:8000/login/',data=data) #post方法有2个参数,一个url一个data print (res.text) #打印数据 print (res) #打印response
运行结果
3.重构发送post请求
import requests url = 'http://127.0.0.1:8000/login/' data = { 'username':'dajiu', 'password':'123456' } def send_post(url,data): #封装方法,定义函数,有2个参数 res = requests.post(url=url,data=data) return res.text #返回结果 print(send_post(url,data)) #打印结果
运行结果:
3.重构发送get请求
#这个方法我得到的结果是返回为空
import requests import json url = 'http://127.0.0.1:8000/login/?' data = { 'username':'dajiu', 'password':'123456' } def send_get(url,data): result = requests.get(url=url,data=data).json() res = json.dumps(result) return res print(send_get(url,data))
第二种
import requests import json url = 'http://127.0.0.1:8000/login/?username=dajiu&password=123456' def send_get(url): res = requests.get(url=url).json() r = json.dumps(res) return r print (send_get(url))
运行结果
4.格式化响应数据
import requests import json url = 'http://127.0.0.1:8000/login/?username=dajiu&password=123456' def send_get(url): res = requests.get(url=url).json() r = json.dumps(res,indent=2,sort_keys=True) #indent=2是空格,sort是排序(按照字母排序) return r print (send_get(url))
运行结果
5.使用类封装测试脚本
先使用一个函数来调用接口函数
import requests import json url = 'http://127.0.0.1:8000/login/?username=dajiu&password=123' data = { 'username':'dajiu', 'password':'123456' } def send_get(url): res = requests.get(url=url).json() r = json.dumps(res,indent=2,sort_keys=True) return r def send_post(url,data): res = requests.post(url=url,data=data) return res.text def run_main(url,method,data=None): #把data数据默认为空,因为get不用传数据,空参数要放在有值参数后面 res = None if method == 'GET': res = send_get(url) else: res = send_post(url,data) return res print(run_main(url,'POST',data))
运行结果
再使用类封装:第一种
import requests import json class RunMain: #当前类的第一个参数是self def send_get(self,url): res = requests.get(url=url).json() r = json.dumps(res,indent=2,sort_keys=True) return r def send_post(self,url,data): res = requests.post(url=url,data=data).json() r = json.dumps(res,indent=2,sort_keys=True) return r def run_main(self,url,method,data=None): #把daa数据默认为空,因为get不用传数据,空参数要放在有值参数后面 res = None if method == 'GET': res = self.send_get(url) else: res = self.send_post(url,data) return res if __name__ == '__main__': #这种写法就是每次都要先实例化,然后再去调用run_main run = RunMain() url = 'http://127.0.0.1:8000/login/?username=dajiu&password=123' data = { 'username':'dajiu', 'password':'123456' } print(run.run_main(url,'POST',data))
运行结果:
第二种
import requests import json class RunMain: #构造函数 def __init__(self,url,method,data=None): self.res=self.run_main(url,method,data) #当前类的第一个参数是self def send_get(self,url): res = requests.get(url=url).json() r = json.dumps(res,indent=2,sort_keys=True) return r def send_post(self,url,data): res = requests.post(url=url,data=data).json() r = json.dumps(res,indent=2,sort_keys=True) return r def run_main(self,url,method,data=None): #把daa数据默认为空,因为get不用传数据,空参数要放在有值参数后面 res = None if method == 'GET': res = self.send_get(url) else: res = self.send_post(url,data) return res if __name__=="__main__": url = 'http://127.0.0.1:8000/login/' data = { 'username': 'sun', 'password': '123', } run=RunMain(url,'POST',data)#实例化的同时执行构造函数 print(run.res)
运行结果: