python - 添加多个身份验证处理程序以请求调用

标签 python python-3.x python-requests

urllib.requests 中,您可以构建开启器,您可以在其中传递多个身份验证处理程序。你能用 Python 请求做同样的事情吗?

请求文档站点上的示例显示一次只传递一个身份验证处理程序,但我需要传递两个。这可能吗?

有人可以提供有关如何执行此操作的语法/文档吗?

这是我在 urllib.requests 包中的做法:

password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)

handler1 = urllib.request.HTTPBasicAuthHandler(password_mgr)
handler2 = urllib.request.HTTPDigestAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener([handler1, handler2])

# use the opener to fetch a URL
opener.open(a_url)

# Install the opener.
# Now all calls to urllib.request.urlopen use our opener.
urllib.request.install_opener(opener)

最佳答案

看来 Requests 不支持这个。我在 github Issue 页面上发布了这个问题。建议使用:http://toolbelt.readthedocs.io/en/latest/authentication.html#guessauth

我还没有尝试过,但它似乎是一个可行的解决方案。

关于python - 添加多个身份验证处理程序以请求调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46154516/

相关文章:

python - asyncio Queue.get 延迟

python - 使用python请求登录网站时返回400

python - Pandas:从以前的时间序列值创建列

python - 未绑定(bind)本地错误 : int vs list

c++ - 如何使用curl将POST请求从python重写为C++

python - 具有多个连接的请求

python - 如何从 Selenium 浏览器加载 session 和 cookie 到 Python 中的请求库?

python - 对于 SSL 和非 SSL 配置,如何使用子域将 nginx 正确设置为 gunicorn 和 flask 的反向代理?

python - 有没有办法在解包时将 splat-assign 分配为元组而不是列表?

python-3.x - 从私有(private) Bitbucket 存储库安装 Python 包