Search code examples
pythonrestcurlpycurl

Get page using NTLM with pyCurl


I am trying to retrieve data from a REST webservice which is hosted on a Windows IIS server; the server uses HTTPS and requires authentication.

The following Curl command works fine:

 curl -k --ntlm --user bob https://mywebservice

However when I come to translate this to the equivalent pycurl so I can use it from within a program I am struggling to get it to authenticate.

So far I have:

import pycurl

name='bob'
pwd='pwd1'
url="https://mywebservice"

curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.SSL_VERIFYPEER, 0)

#not sure about this
#curl.setopt(pycurl.PROXYAUTH, pycurl.HTTPAUTH_NTLM)
#curl.setopt(pycurl.PROXYUSERPWD,"{}:{}".format(name, pwd))

curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_NTLM)
curl.setopt(pycurl.USERNAME, name)
curl.setopt(pycurl.USERPWD, pwd)


curl.perform()
curl.close()

Anyway, with the following code and variations on it, I keep getting:

401 - Unauthorized: Access is denied due to invalid credentials

back from IIS.


Solution

  • The issue was that I was using pycurl.USERNAME, pycurl.USERPWD.

    I had to route through the source but I found: pycurl.USERPWD and that got it working:

    import pycurl
    
    name='bob'
    pwd='pwd1'
    url="https://mywebservice"
    
    curl = pycurl.Curl()
    curl.setopt(pycurl.URL, url)
    curl.setopt(pycurl.SSL_VERIFYPEER, 0)
    
    curl.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_NTLM)
    curl.setopt(pycurl.USERPWD, "{}:{}".format(name, pwd))
    
    curl.perform()
    curl.close()