简体   繁体   English

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

[英]How to disable cookie handling with the Python requests library?

When I use requests to access an URL cookies are automatically sent back to the server (in the following example the requested URL set some cookie values and then redirect to another URL that display the stored cookie) 当我使用请求访问URL时, cookie会自动发送回服务器 (在下面的示例中,请求的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}'

Is it possible to temporary disable cookie handling in the same way you set Chrome or Firefox to not accept cookies? 是否可以像设置Chrome或Firefox不接受cookie一样临时禁用cookie处理?

For example if I access the aforementioned URL with Chrome with cookie handling disabled I get what I expected: 例如,如果我使用Chrome访问上述URL并禁用cookie处理,我会得到我的预期:

{
  "cookies": {}
}

You can do this by defining a cookie policy to reject all cookies: 您可以通过定义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

(Note that http.cookiejar 's API requires you to define a bunch of attributes and methods, as shown.) (请注意, http.cookiejar的API要求您定义一组属性和方法,如图所示。)

Then, set the cookie policy on your Requests session: 然后,在您的请求会话中设置cookie策略:

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

It will now not store or send cookies: 它现在不会存储或发送cookie:

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

As an aside, if you look at the code, the convenience methods in the requests package (as opposed to those on a requests.Session object) construct a new Session each time. 顺便说一句,如果你看一下代码, requests包中的便捷方法(而不是requests.Session对象上的方法)每次都构造一个新的Session Therefore, cookies aren't persisted between separate calls to requests.get . 因此,在对requests.get单独调用之间不会保留cookie。 However, if the first page sets cookies and then issues an HTTP redirect, the target page will see the cookies. 但是,如果第一页设置了cookie然后发出HTTP重定向,则目标页面将看到cookie。 (This is what happens with the HTTPBin /cookies/set call, which redirects to /cookies .) (这是HTTPBin /cookies/set调用所发生的情况,重定向到/cookies 。)

So depending on what behavior you want for redirects, you might not need to do anything special. 因此,根据您想要重定向的行为,您可能不需要做任何特别的事情。 Compare: 相比:

>>> 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': {}}
>>> 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}'

You're getting back "k2": "v2", "k1": "v1" because they're sent in GET params. 你回来了"k2": "v2", "k1": "v1"因为它们是在GET参数中发送的。 If you follow up with a second request you'll see you send no cookies. 如果您跟进第二个请求,您将看到您不发送任何cookie。 Unless you use requests.Session cookies are not automatically handled in the client and you have to explicitly pass a dict or CookieJar with each request. 除非您使用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}'
class BlockAll(CookiePolicy):
    def set_ok(self, cookie, request):
        return False
session.cookies.policy = BlockAll()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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