简体   繁体   中英

Python Session cookies not persisting

I am trying to login to a webpage and then perform a subsequent GET request from the same domain which should use the cookie set from the initial POST.

The initial POST is performed successfully and the response is the expected page, however any subsequent GET requests shoot me back to the login page.

Code:

s = requests.Session()
url1 = 'https://www.sitename.com'
url2 = 'https://www.sitename.com/cookie_dependent_resource'
creds = {'un': 'username', 'pw': 'password'}
r1 = s.post(url, data=creds)
r2 = s.get(url2)

I am new to this but obviously doing something wrong. Help a newbie please! :-)

It turns out the issue was with the server I was connecting to, not my code. I ended up attempting this same operation with a different server and it worked like a charm.

Thanks to everyone who tried to sanity check my work! It was much appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM