requests-ntlm: NTLM Authentication for Requests

1.3.0 · active · verified Thu Apr 09

This package enables HTTP NTLM authentication for the popular Python requests library. It provides a simple `HttpNtlmAuth` class that integrates seamlessly with `requests.Session` for handling NTLM challenges. The current version is 1.3.0, and it follows an active maintenance cadence with releases addressing compatibility, security, and minor enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `HttpNtlmAuth` with a `requests.Session`. It's crucial to correctly format the username (e.g., `DOMAIN\username` or `user@domain.com`) and provide the corresponding password. The example uses environment variables for sensitive data for security and convenience.

import requests
from requests_ntlm import HttpNtlmAuth
import os

# Replace with your NTLM-protected URL
url = os.environ.get('NTLM_TEST_URL', 'http://ntlm-protected-server.local/api/')

# Replace with your NTLM credentials
username = os.environ.get('NTLM_USERNAME', 'DOMAIN\\user')
password = os.environ.get('NTLM_PASSWORD', 'your_password')

try:
    session = requests.Session()
    session.auth = HttpNtlmAuth(username, password)
    response = session.get(url)
    response.raise_for_status() # Raise an exception for bad status codes
    print(f"Successfully authenticated to {url}")
    print("Response content (first 200 chars):\n", response.text[:200])
except requests.exceptions.RequestException as e:
    print(f"Error connecting or authenticating: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →