Python 2.7 如何在python中设置get请求标头中的Cookie

Python 2.7 如何在python中设置get请求标头中的Cookie,python-2.7,header,python-requests,http-headers,request-headers,Python 2.7,Header,Python Requests,Http Headers,Request Headers,我正在尝试对本地内部门户执行GET请求调用,以从门户获取数据。我能够使用下面的代码成功地完成它。但我正在硬编码请求头中的“cookie”参数,该参数将来可能会更改。如果我没有给出标题中的cookies,我会得到403响应。是否仍然可以在不硬编码“cookie”头的情况下执行GET调用 PS-我是以下代码中chrome开发者工具中的“请求头”中提到的头。chrome开发者工具中的请求头: GET /api/router/service/instance-33/api/monitoring/metr

我正在尝试对本地内部门户执行GET请求调用,以从门户获取数据。我能够使用下面的代码成功地完成它。但我正在硬编码请求头中的“cookie”参数,该参数将来可能会更改。如果我没有给出标题中的cookies,我会得到403响应。是否仍然可以在不硬编码“cookie”头的情况下执行GET调用

PS-我是以下代码中chrome开发者工具中的“请求头”中提到的头。chrome开发者工具中的请求头:

GET /api/router/service/instance-33/api/monitoring/metrics/summary?timeline=10m&metricGroupType=Client HTTP/1.1
Host: xxx
Connection: keep-alive
Accept: application/json
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
Content-Type: application/json
Referer: xxx
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cookie: _ga=GA1.2.1166979480.1557148769; _rollup=GA1.2.1925782236.1557148769; _gid=GA1.2.572392410.1560761227; VIDUSR=1560762756-eXdFaeT29yEDoA%3d%3d
代码im使用:

import requests

url = 'xxx'
proxies = {
  'xxx/',
  'xxx',
}

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36', 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9', 'Connection': 'keep-alive', 'Content-Type': 'application/json', 'Cookie': '_ga=GA1.2.1166979480.1557148769; _rollup=GA1.2.1925782236.1557148769; _gid=GA1.2.572392410.1560761227; VIDUSR=1560762756-eXdFaeT29yEDoA%3d%3d', 'Host': 'xxx', 'Referer': 'xxx', 'X-Requested-With': 'XMLHttpRequest'}

r = requests.get(url, proxies=proxies, verify=False, headers=headers)

print(r.json())
print(r)

我找到了答案。与其在标题中提供cookie详细信息,不如提供用户名和密码,并在GET请求中进行身份验证。我找到了答案。与其在标题中提供cookie详细信息,不如提供用户名和密码,并在GET请求中进行身份验证。