问题
I am trying to convert an R program into Python. The R program uses the following code to pass a request to the server:
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")
This works exactly as expected. Here's what I have with 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)
This gives me an html file back that says "401 - Unauthorized: Access is denied due to invalid credentials." I have tried a bunch of variations of this to no avail. What could the reason behind this be?
回答1:
You can try HTTPProxyAuth
import requests
from requests.auth import HTTPProxyAuth
response = requests.post(url_base, data = url_options, auth = HTTPProxyAuth("username", "password"))
来源:https://stackoverflow.com/questions/57081162/unable-to-pass-proper-credentials-to-server-through-corporate-proxy-in-python-b