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

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

问题描述

当我使用请求访问 URL 时 cookie 会自动发送回服务器(在下面的示例中,请求的 URL 设置了一些 cookie 值,然后重定向到另一个显示存储的 cookie 的 URL)

<预><代码>>>>进口请求>>>response = requests.get("http://httpbin.org/cookies/set?k1=v1&k2=v2")>>>响应内容'{ "cookies": { "k2": "v2", "k1": "v1" } }'

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

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

<代码>{饼干": {}}

解决方案

您可以通过定义 cookie 策略来拒绝所有 cookie:

from http import cookiejar # Python 2: import cookielib as cookiejar类 BlockAll(cookiejar.CookiePolicy):return_ok = set_ok = domain_return_ok = path_return_ok = lambda self, *args, **kwargs: False网景=真rfc2965 = hide_cookie2 = 假

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

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

导入请求s = requests.Session()s.cookies.set_policy(BlockAll())

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

s.get("https://httpbin.org/cookies/set?foo=bar")断言不是 s.cookies

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

因此,根据您希望重定向的行为,您可能不需要做任何特殊的事情.比较:

<预><代码>>>>打印(requests.get("https://httpbin.org/cookies/set?foo=bar").json()){'cookies': {'foo': 'bar'}}>>>打印(requests.get("https://httpbin.org/cookies").json()){'饼干': {}}>>>s = requests.Session()>>>打印(s.get("https://httpbin.org/cookies/set?foo=bar").json()){'cookies': {'foo': 'bar'}}>>>打印(s.get(https://httpbin.org/cookies").json()){'cookies': {'foo': 'bar'}}>>>s = requests.Session()>>>s.cookies.set_policy(BlockAll())>>>打印(s.get("https://httpbin.org/cookies/set?foo=bar").json()){'饼干': {}}>>>打印(requests.get("https://httpbin.org/cookies").json()){'饼干': {}}

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
'{
  "cookies": {
    "k2": "v2",
    "k1": "v1"
  }
}'

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

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

{
  "cookies": {}
}

解决方案

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

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

Then, set the cookie policy on your Requests session:

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

It will now not store or send cookies:

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

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

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