Interface test of requests to add cookie Login

What use cookies? What is the difference with session details, please see:? Https://blog.csdn.net/xxlovesht/article/details/80918151

A cookie is a client browser to send a text string handle, and saved on the client hard disk that can be used during a session lasting WEB SITE maintain data.

session actually refers to the visitors reach a certain page from that time to leave until. Session Cookie is actually using the information processing, when the user first was a request, the server creates a Cookie on the user's browser, when the end of this Session, in fact, means that the Cookie will expire.
Note: Cookie name for this user-created is aspsessionid. Cookie sole purpose of this is to provide different authentication for each user.

cookie and session have in common is: cookie and session are used to track the user's identity conversational mode browser.

The difference between cookie and session are: cookie stored in the client data, session data is stored on the server side.

First, access to cookie

 

Second, the use cookie

(1) get requests directly or post added cookies, only need to add the name and value of cookies values, the dictionary is added cookie mode

cookie={

name:value

}
Url = "XXXXXXXXXXXX" # URL success can only log in to view
S = requests.session ()
Re = s.get (url = url, Cookies = the cookie)

(2) headers added cookie

headers = {

'User-Agent':'xxxxxxxxxxxxxx',

'cookie':'sid=xxxxxxx'

}
response = requests.post(url,data=data,headers=headers)

After the Mu class is over selenium Netcom successful login, access to muke.json cookie to save the file, add the cookie by reading the file

To Mu class network, for example, the following cookie file Mu class web content:

Login code to achieve the following:

url="https://m.imocc.com/password/user/login"
dict_cookie={}
with open("d:/muke.json",'r',encoding="utf-") as f:
    cookies=json.loads(f.read())
for i in cookies:
    if i['name']=="apsid":
        dict_cookie=i
cookie={
    dict_cookie['name']:dict_cookie['value']
    }
url="https://www.imooc.com/u/index/follows"
s = requests.session()
re=s.get(url=url,cookies=cookie)
print (re.text)

 

 

 

Guess you like

Origin blog.csdn.net/qq_35577990/article/details/89819846