简体   繁体   中英

Python Request module not getting cookie from response

I'm writing code trying to get Cookies from webserver using Request module, but what confused me is that:

  1. I'm seeing Cookies returned when I tested using PostMan - REST Client
  2. I didn't sent any cookies in my sent request, but what surprised me is that could find the cookie I want in Sent requests.

I want to get cookie data that I could use in code to request another application.

Following is my code:

import requests
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()

username = 'user123'  # real username/password not showing
password = '1234567'

login_data = {'id':username, 'pass_word':password, 'action': 'login'}

r = requests.post("www.example.com/login/", data=login_data)

print r.cookies 
print r.request.header['Cookie']

Output:

<<class 'requests.cookies.RequestsCookieJar'>[]>   # why nothing??

{'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.6.0 CPython/2.7.6 Darwin/14.1.0', 'Connection': 'keep-alive', 'Cookie': 'blahblahblahblah', 'Content-Type': 'application/x-www-form-urlencoded'}

For confidential reason, I cannot show my real cookies data here, but I do see it's in my send request, how come? I didn't tell it to send it in my request, and this is the data that i expect to get from response cookie, but it showed as none.

The r.cookies jar contains all cookies that are new, that the server has sent in the current response, so any cookies that have been sent will not appear there unless the server resent them. The sent cookies will appear in r.request.headers (the jar is at r.request._cookies ). If you want to preserve the cookies across multiple requests, use a Session :

session = requests.Session()
session.get(...)  # Your first request
session.get(...)  # Following requests pass the cookies on

While using the session, you can retrieve any cookies by looking in session.cookies . I don't know why you saw cookies sent when you didn't, I'd have to see more code.

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