天天看點

python發送cookie請求_Python請求發送多個Cookie

python發送cookie請求_Python請求發送多個Cookie

How would you go about sending multiple cookies with python requests

the documentation only gives

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"

the security cookie is not recognised but the PHPSESID is

解決方案

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) 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) 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)