python httplib2 : HTTPS login fails

标签 python http https httplib2

我正在尝试使用 httplib2登录网页。我只需在 Chrome 隐身窗口中打开以下 URL 即可登录该页面:

https://domain.com/auth?name=USERNAME&pw=PASSWORD

我尝试了以下代码来使用 httplib2 模拟此登录:

from httplib2 import Http
h = Http(disable_ssl_certificate_validation=True)
resp, content = h.request('https://domain.com/auth?name=USERNAME&pw=PASSWORD')

不幸的是,这个请求不会导致成功登录。

我尝试更改请求 header 以匹配 Chrome 提供的请求 header :

headers = {
    'Host': 'domain.com',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
    'Accept-Encoding': 'gzip,deflate,sdch',
    'Accept-Language': 'en-US,en;q=0.8'
}
resp, content = h.request('https://domain.com/auth?name=USERNAME&pw=PASSWORD', 'GET', headers=headers)

这会稍微改变响应,但仍不会导致成功登录。

我尝试使用 Wireshark 检查实际网络流量但由于它是 HTTPS 并因此被加密,所以我看不到实际流量。

有人知道 Chrome 和 httplib2 之间的请求有什么区别吗?也许 httplib2 改变了我的一些标题?

最佳答案

根据 Games Brainiac 的评论,我最终简单地使用了 Python Requests而不是 httplib2。以下 requests 代码开箱即用:

import requests
session = requests.Session()
response = session.get('https://domain.com/auth?name=USERNAME&pw=PASSWORD')

可以简单地在 Session 对象上执行具有相同用户名/密码的其他请求:

...
next_response = session.get('https://domain.com/someOtherPage')

关于 python httplib2 : HTTPS login fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566645/

相关文章:

ssl - 如果我只将 https 用作 REST 端点,我可以使用自签名证书吗?

python - 在 Python 中使用 cx_Oracle 和 xlrd 的带有列表列表的 executemany() 返回 TypeError

php - 从 Python 到 PHP 的 GREP 函数

c - 我写了一个简单的 http 服务器,似乎可以工作,但在尝试通过浏览器访问时遇到问题

json - 通过 header 错误代码返回或 JSON 响应终止 http 请求

ssl - 将 https 客户端的 header 转发到后端应用程序

Android: java.lang.IllegalStateException: Scheme 'http' 未注册 - HTTPS 调用错误

python - 使用 hue 参数在 seaborn 中拆分 fiddle 图

python - 有没有办法将数字转换为颜色

windows - 谷歌去 : Why does the http server package not serve more than 5 simultaneous requests?