如何使用Python请求库禁用cookie处理?

当我使用请求访问URL cookies are automatically sent back to the server 时(在以下示例中,请求的URL设置了一些cookie值,然后重定向到显示存储的cookie的另一个URL)

>>> import requests
>>> response = requests.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")
>>> response.content
'{\n  "cookies": {\n    "k2": "v2",\n    "k1": "v1"\n  }\n}'

是否可以像设置Chrome或Firefox不接受cookie一样临时禁用cookie处理?

例如,如果我使用Chrome访问上述URL并禁用cookie处理,我会得到我的预期:

{
  "cookies": {}
}

回答(4)

2 years ago

您可以通过定义cookie策略来拒绝所有cookie来执行此操作:

from http import cookiejar  # Python 2: import cookielib as cookiejar
class BlockAll(cookiejar.CookiePolicy):
    return_ok = set_ok = domain_return_ok = path_return_ok = lambda self, *args, **kwargs: False
    netscape = True
    rfc2965 = hide_cookie2 = False

(注意 http.cookiejar 的API要求您定义一组属性和方法,如图所示 . )

然后,在您的请求会话中设置cookie策略:

import requests
s = requests.Session()
s.cookies.set_policy(BlockAll())

它现在不会存储或发送cookie:

s.get("https://httpbin.org/cookies/set?foo=bar")
assert not s.cookies

顺便说一句,如果你看一下代码, requests 包中的便捷方法(而不是 requests.Session 对象上的方法)每次构造一个新的 Session . 因此,在 requests.get 的单独调用之间不会保留cookie . 但是,如果第一页设置了cookie然后发出HTTP重定向,则目标页面将看到cookie . (这是HTTPBin /cookies/set 调用所发生的情况,它会重定向到 /cookies . )

因此,根据您想要重定向的行为,您可能不需要做任何特别的事情 . 相比:

>>> print(requests.get("https://httpbin.org/cookies/set?foo=bar").json())
{'cookies': {'foo': 'bar'}}
>>> print(requests.get("https://httpbin.org/cookies").json())
{'cookies': {}}

>>> s = requests.Session()
>>> print(s.get("https://httpbin.org/cookies/set?foo=bar").json())
{'cookies': {'foo': 'bar'}}
>>> print(s.get("https://httpbin.org/cookies").json())
{'cookies': {'foo': 'bar'}}

>>> s = requests.Session()
>>> s.cookies.set_policy(BlockAll())
>>> print(s.get("https://httpbin.org/cookies/set?foo=bar").json())
{'cookies': {}}
>>> print(requests.get("https://httpbin.org/cookies").json())
{'cookies': {}}

2 years ago

>>> import mock
>>> import requests
>>> with mock.patch.object(requests.cookies.RequestsCookieJar, 'update', lambda *args, **kwargs: 0):
...     r = requests.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")#, cookies=cj)
...     r.content
... 
'{\n  "cookies": {}\n}'
>>> r = requests.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")
>>> r.content
'{\n  "cookies": {\n    "k2": "v2",\n    "k1": "v1"\n  }\n}'

2 years ago

你回来了 "k2": "v2", "k1": "v1" 因为他们're sent in GET params. If you follow up with a second request you' ll看到你发送没有cookie . 除非您使用 requests.Session ,否则客户端不会自动处理cookie,您必须为每个请求显式传递dict或CookieJar .

In [17]: r = requests.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")

In [18]: r.content
Out[18]: '{\n  "cookies": {\n    "k2": "v2",\n    "k1": "v1"\n  }\n}'

In [20]: r.cookies.get_dict()
Out[20]: {}

In [21]: r = requests.get("http://httpbin.org/cookies")

In [22]: r.content
Out[22]: '{\n  "cookies": {}\n}'

2 years ago

class BlockAll(CookiePolicy):
    def set_ok(self, cookie, request):
        return False
session.cookies.policy = BlockAll()