1. 程式人生 > >Python模擬接口登錄

Python模擬接口登錄

實現 cep split 轉碼 我們 span html nec Coding

參考地址:https://blog.csdn.net/rifengxxc/article/details/77414090

下面講下關於python模擬登錄實驗,之前怎麽調試也不行,我也是摸索了好久,結合網上一些資料,終於把我們的系統實現了登錄操作。
首先,我們的系統有重定向跳轉,先post請求302,然後進行get請求;
以下代碼僅供參考!

#_*_coding:utf-8_*_
import requests,base64
# var=base64.b64encode("123456") #轉碼
# print var
url = "http://10.XXX.XXX.XXX:8080/"
def get_cookie(): #獲取cookie方法
get_cookie = requests.get(url).headers["Set-Cookie"].split(";")[0]
return get_cookie
def login(url_login): #登錄方法
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "http://10.41.252.111:8080/",
"Accept-Encoding": "gzip, deflate",
"Host": "10.XXX.XXX.XXX:8080",
"Origin": "http://10.XXX.XXX.XXX:8080",
"Connection": "keep-alive",
"Cookie": get_cookie()
}
body = "username=用戶名&password=密碼&rememberMe=false"
reps=requests.post(url,data=body,headers=header,allow_redirects=False) # 設置 allow_redirects=False 使得禁止重定向
r=requests.get(url_login,headers=header ) #進行get請求的提交登錄
print r.text
login("http://10.XXX.XXX.XXX:8080/index") #調用登錄方法,該地址是點擊登錄請求的url,註意路徑/index



Python模擬接口登錄