python - HTTPCookieProcessor 未将 cookie 添加到请求标头
问题描述
我正在尝试设置HTTP cookiePOST
的网页。JSESSIONID
尝试使用 自动设置此 cookie 时HTTPCookieProcessor
,页面给出空白响应,当我打印出请求标头时,页面Cookie
丢失。
代码:
@contextmanager
def get_opener():
global proxies
proxy = proxies.get()
try:
yield request.build_opener(
request.HTTPCookieProcessor(),
request.ProxyHandler(proxy)
)
finally:
proxies.put(proxy)
def download(data, destination, tries=0):
with get_opener() as opener:
first_response = opener.open(request.Request('https://example.org/page.html'))
print('First response info:')
print(first_response.info())
req = request.Request(Config.DOWNLOAD_URL, data=data, method='POST')
response = opener.open(req)
print('Second response:')
print(response.info())
print(response.read())
print(req.headers)
输出:
First response info:
Set-Cookie: JSESSIONID=04CBF51EE096C4AD382D7D556CDF356D; Path=/; Secure; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en-US
Transfer-Encoding: chunked
Vary: Accept-Encoding
Date: Wed, 24 Jul 2019 05:37:05 GMT
Connection: close
Second response:
Content-Length: 0
Date: Wed, 24 Jul 2019 05:37:06 GMT
Connection: close
b''
{} # note no 'Cookie' header
我的印象是 cookie 会自动设置:
解决方案
推荐阅读
- spring - Spring-security oauth2 自定义 AuthenticationProvider
- c# - 如何在循环中将 EventHandler 分配给动态对象?
- c++ - 使用模板化函数 c++ 从副本中保存“新”对象
- javascript - PhoneGap (Android):将 localStorage 备份到云的最简单、最快的方法?
- wordpress - 如何将带有文本字段的图像插入数据库的自定义表中?
- qt - qglviewer上鼠标下的坐标错误
- vhdl - 错误 (10536):counts.vhd(25) 处的 VHDL 循环语句错误:循环必须在 10,000 次迭代内终止
- python - 导航面板未显示在大图上
- java - 如何去除弹出窗口和工具提示周围的灰色阴影?
- python - 如果没有 python 3.5 或更高版本和 pip 18 及更高版本,MatPlotLib 将无法安装,而且我没有 root 访问权限