背景:
实际开发中,Python程序中需要调用后台接口,充当前端,后端规定请求头需要携带token
封装的get和post类:
class RequestMethodCarryJson:
"""
定义请求类型
以json方式传递参数
"""
def __init__(self):
"""初始化参数"""
self.data = {}
self.files = {}
def get(self, url, data, headers):
"""
定义get方法请求
:return:
"""
try:
return requests.get(url=url, data=data, headers=headers, timeout=60)
except TimeoutError:
return print('%s get request timeout!' % url)
def getCarryToken(self, url, data, headers):
"""
定义get方法请求
:return:
"""
try:
return requests.get(url=url, json=data, headers=headers, timeout=60)
except TimeoutError:
return print('%s get request timeout!' % url)
def post(self, url, data, headers):
"""
定义post方法请求
post携带token,看起来不需要像get那样添加一个getCarryToken特有的识别方法
:return:
"""
try:
return requests.post(url=url, data=json.dumps(data), headers=headers, timeout=60)
except TimeoutError:
return print('%s post request timeout!' % url)
class RequestMethodCarryFormData:
"""
定义请求类型
以表单方式form-data传递参数
"""
def __init__(self):
"""初始化参数"""
self.data = {}
self.files = {}
def get(self, url, data, headers):
"""
定义get方法请求
:return:
"""
try:
return requests.get(url=url, data=data, headers=headers, timeout=60)
except TimeoutError:
return print('%s get request timeout!' % url)
def getCarryToken(self, url, data, headers):
"""
定义get方法请求,额外添加token
:return:
"""
try:
return requests.get(url=url, json=data, headers=headers, timeout=60)
except TimeoutError:
return print('%s get request timeout!' % url)
def post(self, url, data, headers):
"""
定义post方法请求
这个携带json应该不需要额外改
:return:
"""
try:
return requests.post(url=url, data=data, headers=headers, timeout=60)
except TimeoutError:
return print('%s post request timeout!' % url)
应用场景:
场景1——get请求时Headers携带token[传递参数以json格式]:
token="里面填token内容"
test1Info = test1(token)
def test1(token):
"""
携带token,
访问平台已经存在的数据库,
以json格式传递数据
:param token:
:return:
"""
url = "http://127.0.0.1:8088/backup/url1"
headers = {'Content-Type': 'application/json;charset=utf-8', 'token': token}
data = dict()
data["param1"] = "param1"
data["param2"] = "param2"
resp = RequestMethodCarryJson().getCarryToken(url, data, headers).json()
dbInfo = resp["items"]
return dbInfo
场景2——post请求时Headers携带token[传递参数以application/x-www-form-urlencoded(表单)格式]:
token="里面填token内容"
test2Info = test2(token)
def test2(token):
url = "http://127.0.0.1:8088/testFormData/json/transfer"
headers = {'Content-Type': 'application/x-www-form-urlencoded', 'token': token}
data = dict()
data["param1"] = "param1"
"""
这里不接受状态码
"""
resp = RequestMethodCarryFormData().post(url, data, headers).json()
info = resp["items"]
if info == "格式正确":
return True
else:
return False
场景3——踩坑后总结成功的代码:
"""
请求头携带token拿取信息:
1-post-以json格式传递数据,请求头携带token成功
形如:
# resp = requests.post(url,json=data,headers=headers).json()
resp = RequestMethodCarryJson().post(url, data, headers).json()
2-get-以json格式传递数据,请求头携带token成功
# resp = requests.get(url,json=data,headers=headers).json()
resp = RequestMethodCarryJson().getCarryToken(url, data, headers).json()
:param platformInfo:
:return:
"""
延伸理解:
相关资料参考:
postman中 form-data、x-www-form-urlencoded的区别_叫我峰兄的博客-CSDN博客
python requests 带请求头Token发起http请求_python request token_软件测试李同学的博客-CSDN博客
python发送requests请求时,使用登录的token值,作为下一个接口的请求头信息 - 大海一个人听 - 博客园 (cnblogs.com)