如何使用Python Requests库在发布请求中发送Cookie?

问题:如何使用Python Requests库在发布请求中发送Cookie?

我正在尝试使用Requests库发送带有后期请求的cookie,但是我不确定如何根据其文档实际设置cookie。该脚本可在Wikipedia上使用,并且需要发送的cookie具有以下形式:

enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly

但是,requests文档快速入门仅以此为例:

cookies = dict(cookies_are='working')

如何使用该库对上述Cookie进行编码?我是否需要使用python的标准cookie库进行制作,然后将其与POST请求一起发送?

I’m trying to use the Requests library to send cookies with a post request, but I’m not sure how to actually set up the cookies based on its documentation. The script is for use on Wikipedia, and the cookie(s) that need to be sent are of this form:

enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly

However, the requests documentation quickstart gives this as the only example:

cookies = dict(cookies_are='working')

How can I encode a cookie like the above using this library? Do I need to make it with python’s standard cookie library, then send it along with the POST request?


回答 0

最新版本的“请求”将通过简单的词典为您构建CookieJars。

import requests

cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookies)

请享用 :)

The latest release of Requests will build CookieJars for you from simple dictionaries.

import requests

cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookies)

Enjoy :)


回答 1

只是为了扩展上一个答案,如果将两个请求链接在一起,并且想要将第一个返回的cookie发送到第二个(例如,保持跨请求的会话有效),则可以执行以下操作:

import requests
r1 = requests.post('http://www.yourapp.com/login')
r2 = requests.post('http://www.yourapp.com/somepage',cookies=r1.cookies)

Just to extend on the previous answer, if you are linking two requests together and want to send the cookies returned from the first one to the second one (for example, maintaining a session alive across requests) you can do:

import requests
r1 = requests.post('http://www.yourapp.com/login')
r2 = requests.post('http://www.yourapp.com/somepage',cookies=r1.cookies)

回答 2

如果要将cookie传递给浏览器,则必须附加到标头以发送回。如果您使用的是wsgi:

import requests
...


def application(environ, start_response):
    cookie = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}
    response_headers = [('Content-type', 'text/plain')]
    response_headers.append(('Set-Cookie',cookie))
...

    return [bytes(post_env),response_headers]

通过将auth user / password传递给我的python脚本并将cookie传递给浏览器,我能够成功地通过托管在同一域中的Bugzilla和TWiki进行身份验证,而python wsgi脚本正在运行。这使我可以在同一浏览器中打开Bugzilla和TWiki页面并进行身份验证。我正在尝试对SuiteCRM执行相同的操作,但是即使SuiteCRM已成功通过身份验证,我也无法接受从python脚本获得的会话cookie。

If you want to pass the cookie to the browser, you have to append to the headers to be sent back. If you’re using wsgi:

import requests
...


def application(environ, start_response):
    cookie = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}
    response_headers = [('Content-type', 'text/plain')]
    response_headers.append(('Set-Cookie',cookie))
...

    return [bytes(post_env),response_headers]

I’m successfully able to authenticate with Bugzilla and TWiki hosted on the same domain my python wsgi script is running by passing auth user/password to my python script and pass the cookies to the browser. This allows me to open the Bugzilla and TWiki pages in the same browser and be authenticated. I’m trying to do the same with SuiteCRM but i’m having trouble with SuiteCRM accepting the session cookies obtained from the python script even though it has successfully authenticated.