cookie实例
session = requests.session()
#验证码,我们测试环境用的万能验证码
validate_code = '1234'
# 获取登录认证SID
#url地址
url_login = f"http://ops-web-{env}/login.jsp"
#获取cookie,并转换成json格式
sid = requests.get(url=url_login).cookies.get_dict()
# 登录鉴权认证SID
data_login = f'loginName={login_name}&loginPassword={passwd}={validate_code}'
#请求头加入cookie信息,一般使用cookie中某一个字段值看业务需求
headers_login = {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'Cookie': f'sid={sid}'
}
session.post(url=url_login, data=data_login, headers=headers_login)
return f'sid={sid}'
token实例
import requests
import json
#url
url = "http://10.90.12.148:8001/api/login/"
#body
payload = json.dumps({
"username": "rS4UpLq8/t51kT9B08ZniSzfwmm7wZ9QVQ/SlnVGNCTSd/+oC1VRC3J5FDQ63cqnCjJfVMGjhkCcGOLTlSkVLtw9nwnrdNkMwBxdisdqO9oAY/aRVajBizmlIjQLZRqPHdi+sC5M2WPkkRE/hKOfJ/jbRuQleTK/XZ2MC3F7CLg=",
"password": "ST+odGth5pr/sCt+/w+vs1pFDFaUISJ7El61ySPpM3sL0zRe6UpfhC0oG65kEKx/7jPtwa/ROBIxR5QRlL7T7EV0Q1Y9rJ75AkQ4/VtAEoiOMGo0k44R1zi/nuEK6/dx2vNNvwtO89Lc/KmGzZ3TB/w6hd9un9xV9I5fJ3QNUxg="
})
headers = {
'token': 'ZXlKMGVYQWlPaUpLVjFBaUxDSmhiR2NpT2lKa1pXWmhkV3gwSW4wOjFxQjNNNDpRMmJmUURCMXBod1pSc0JfeDRMLXJVV0l4NG1ha3BiSTM0bi1rM2hZQjNr.ZXlKMWMyVnlibUZ0WlNJNklucG9iM1YzWVc1bklpd2lhV0YwSWpveE5qZzNNVE0yT0RFeUxqZzFNelEzTVN3aVpYaHdJam94TmpnNU56STRPREV5TGpnMU16UTNNWDA6MXFCM000OlUycFpobGFVWFdyM3N4aXJKem1KNW1kLURDYkgtVDV0WE1jVUxRQjZyZ2M.3880598c7641a7499f64edc56c890b21',
'Username': 'zhouwang',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
登陆成功服务器会返回token给响应体,其他请求就爱header里面带上token即可