Python请求发送多个cookie

时间:2017-03-15 05:24:35

标签: python cookies python-requests

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

文档仅提供

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是

1 个答案:

答案 0 :(得分:1)

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

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

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标题:

import requests
from Cookie import SimpleCookie

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