Unable to pass proper credentials to server through corporate proxy in python, but able to do it in R

六眼飞鱼酱① 提交于 2019-12-13 01:17:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!