【问题标题】:Unable to pass proper credentials to server through corporate proxy in python, but able to do it in R无法通过python中的公司代理将正确的凭据传递给服务器,但能够在R中做到这一点
【发布时间】:2019-07-17 17:17:04
【问题描述】:

我正在尝试将 R 程序转换为 Python。 R 程序使用以下代码向服务器传递请求:

auth <- function() {
    httr::authenticate(Sys.getenv("proxy_usr"), 
                   Sys.getenv("proxy_auth"), type = "ntlm")
}

raw_data <- httr::POST(url_base, body = url_options, auth()) %>% 
    httr::content("text")

这完全符合预期。以下是我对 Python 的看法:

import os
import requests
from requests_ntlm import HttpNtlmAuth 
user = os.environ.get('proxy_usr', "")
auth_pass = os.environ.get('proxy_auth', "")
response = requests.post(url_base, data = url_options, auth = 
    HttpNtlmAuth(user, auth_pass))
return (response.text)

这给了我一个 html 文件,上面写着“401 - 未经授权:由于凭据无效,访问被拒绝”。我已经尝试了很多这种变化无济于事。这可能是什么原因?

【问题讨论】:

    标签: python r authentication ntlm


    【解决方案1】:

    你可以试试 HTTPProxyAuth

    import requests
    from requests.auth import HTTPProxyAuth
    
    response = requests.post(url_base, data = url_options, auth = HTTPProxyAuth("username", "password"))
    

    【讨论】:

    • 嗨!谢谢您的答复。不幸的是,它没有用。我得到了和以前一样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2012-01-16
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多