HTTPS request via urllib2 fails behind NTLM proxy

后端 未结 1 1462
有刺的猬
有刺的猬 2021-01-28 19:32

Via Python\'s urllib2 I try to get data over HTTPS while I am behind a corporate NTLM proxy.

I run

proxy_url = (\'http://user:pw@ntlmproxy:p         


        
1条回答
  •  抹茶落季
    2021-01-28 20:00

    The problem is that Python's standard HTTP libraries do not speak Microsoft's proprietary NTLM authentication protocol fully.

    I solved this problem by setting up a local NTLM-capable proxy - ntlmaps did the trick for me.(*) - which providesthe authentication against the corporate proxy and point my python code to this local proxy without authentication credentials.

    Additionally I had to add in the above listed python code a proxy_handler for HTTPS. So I replaced the two lines

    proxy_url = 'http://user:pw@ntlmproxy:port/' 
    proxy_handler = urllib2.ProxyHandler({'http': proxy_url})
    

    with the two lines

    proxy_url = 'http://localproxy:localport/' 
    proxy_url_https = 'https://localproxy:localport/' 
    proxy_handler = urllib2.ProxyHandler({'http': proxy_url, 'https': proxy_url_https})
    

    Then the request works perfectly.


    (*) ntlmaps is a Python program. Due to some reasons in my personal environment it was for me necessary that the proxy is a python program.)

    0 讨论(0)
提交回复
热议问题