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

查看:665
本文介绍了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

这是我目前尝试过的内容

Here is what i have tried currently

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)

当我使用

when i run my script with

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

安全cookie未被识别,但PHPSESID为

the security cookie is not recognised but the PHPSESID is

推荐答案

cookies 参数是一个字典,其中的键是cookie名称,值是cookie值。因此,虽然您的cookie输入是cookie字符串,但是您可以通过两种方式将其与请求一起使用:

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字符串,并从中进行排序,如下所示:

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天全站免登陆