python - Using requests module, how to handle 'set-cookie' in request response? -
i'm attempting open login page (get), fetch cookies provided webserver, submit username , password pair log site (post).
looking @ this stackoverflow question/answer, think following:
import requests import cookielib url1 = 'login prompt page' url2 = 'login submission url' jar = cookielib.cookiejar() r = requests.get(url1, cookies=jar) r2 = requests.post(url2, cookies=jar, data="username , password data payload")
however, in r
there set-cookie
in header, isn't changing in jar
object. in fact, nothing being populated jar
linked question's response indicate.
i'm getting around in code having headers dict , after doing or post, using handle set-cookie
header:
headers['cookie'] = r.headers['set-cookie']
then passing around header in requests methods. correct, or there better way apply set-cookie
?
ignore cookie-jar, let requests
handle cookies you. use session object instead, it'll persist cookies , send them server:
with requests.session() s: r = s.get(url1) r = s.post(url2, data="username , password data payload")
Comments
Post a Comment