Python 请求发送多个 cookie [英] Python requests send multiple cookies

查看:39
本文介绍了Python 请求发送多个 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何使用 python 请求发送多个 cookie

How would you go about sending multiple cookies with python requests

文档只给出了

url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)
r.text

这是我目前尝试过的

url = 'http://192.168.0.57/dvwa/vulnerabilities/fi/?page=include.php'
cookies = dict(cookies_are=options.cookie)
r = requests.get(url, cookies=cookies, headers=headers)

当我用

--cookie="security=Low; PHPSESSID=4edca0525666fbbe319f50ce3630b4a9"

无法识别安全 cookie 但 PHPSESID 是

the security cookie is not recognised but the PHPSESID is

推荐答案

cookies 参数是一个字典,其中键是 cookie 名称,值是 cookie 值.因此,虽然您的 cookie 输入是 cookie 字符串,但您有两种方法可以将它与 requests 一起使用:

The cookies parameter is a dict where the key is cookie name and the value is cookie value. So while your cookie-input is cookie string you have two ways to use it with requests:

1) 解析 cookie 字符串并从中生成 dict,如下所示:

1) parse cookie string and make the dict from it like the following:

import requests
from Cookie import SimpleCookie

cookie_string = options.cookie
cookie = SimpleCookie(cookie_string)
cookie_dict = {k: v.value for k, v in cookie.iteritems()}
requests.get(url, cookies=cookie_dict)

2) 只需将其设置为 Cookie 标头:

2) simply set it as Cookie header:

import requests
from Cookie import SimpleCookie

cookie_string = options.cookie
headers = {'Cookie': cookie_string}
requests.get(url, headers=headers)

这篇关于Python 请求发送多个 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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