如何禁用Cookie处理与Python请求库? [英] How to disable cookie handling with the Python requests library?

查看:1476
本文介绍了如何禁用Cookie处理与Python请求库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用请求访问网址时,Cookie会自动发送回服务器(在以下示例中,请求的网址设置了一些Cookie值,然后重定向到显示存储的Cookie的其他网址)

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)

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

是否可以临时禁用Cookie处理,方法与将Chrome或Firefox设置为不接受Cookie相同?

Is it possible to temporary disable cookie handling in the same way you set Chrome or Firefox to not accept cookies?

例如,如果我访问前面提到的带有cookie处理功能的Chrome的URL,我得到了我的预期:

For example if I access the aforementioned URL with Chrome with cookie handling disabled I get what I expected:

{
  "cookies": {}
}


推荐答案

您可以通过定义一个cookie策略来拒绝所有cookie来实现此目的:

You can do this by defining a cookie policy to reject all cookies:

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要求您定义一堆属性和方法,如下所示。)

(Note that http.cookiejar's API requires you to define a bunch of attributes and methods, as shown.)

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

Then, set the cookie policy on your Requests session:

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

现在不会存储或发送cookie:

It will now not store or send cookies:

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

另外,如果你看看代码,请求中的方便方法包( requests.Session 对象上的那些)构造一个新的 Session 每一次。因此,cookie不会在对 requests.get 的单独调用之间持久化。但是,如果第一个页面设置Cookie,然后发出HTTP重定向,目标页面将看到Cookie。 (这是HTTPBin / cookies / set 调用发生的,它重定向到 / 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. Therefore, cookies aren't persisted between separate calls to requests.get. However, if the first page sets cookies and then issues an HTTP redirect, the target page will see the cookies. (This is what happens with the HTTPBin /cookies/set call, which redirects to /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': {}}

这篇关于如何禁用Cookie处理与Python请求库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆