Skip to main content

requests-ntlm2

NTLM authentication plugin for Requests

Build Status Python Version PyPI Status Downloads Licence

requests-ntlm2, which is based on requests-ntlm, allows for HTTP NTLM authentication using the requests library.

Installation

pip install requests-ntlm2

Usage

Import classes from the module that defines them. The package root loads no authentication or proxy code.

Class Import from
HttpNtlmAuth requests_ntlm2.auth
NtlmCompatibility requests_ntlm2.compatibility
HttpProxyAdapter requests_ntlm2.proxy.adapters.base
HttpNtlmAdapter requests_ntlm2.proxy.adapters.ntlm
HTTPConnection, HTTPSConnection, VerifiedHTTPSConnection requests_ntlm2.proxy.connection

Basic Usage

HttpNtlmAuth extends requests AuthBase, so usage is simple:

import requests
from requests_ntlm2.auth import HttpNtlmAuth

auth=HttpNtlmAuth('domain\\username','password')
requests.get("http://ntlm_protected_site.com", auth=auth)

Changing NTLM compatibility level

See this MS doc on LM compatibility levels. requests_ntlm2 defaults to compatibility level 3 which supports NTLMv2 [only]. You can change the compatibility level as follows:

import requests
from requests_ntlm2.auth import HttpNtlmAuth
from requests_ntlm2.compatibility import NtlmCompatibility

username = 'domain\\username'
password = 'password123'
ntlm_compatibility = NtlmCompatibility.LM_AND_NTLMv1_WITH_ESS  # => level 1
auth=HttpNtlmAuth(username, password, ntlm_compatibility=ntlm_compatibility)

requests.get("http://ntlm_protected_site.com", auth=auth)

Using with Requests Session

HttpNtlmAuth can be used in conjunction with a Session in order to make use of connection pooling. Since NTLM authenticates connections, this is more efficient. Otherwise, each request will go through a new NTLM challenge-response.

import requests
from requests_ntlm2.auth import HttpNtlmAuth

session = requests.Session()
session.auth = HttpNtlmAuth('domain\\username','password')
session.get('http://ntlm_protected_site.com')

HTTP CONNECT Usage

When using requests-ntlm2 to create SSL proxy tunnel via HTTP CONNECT, the so-called "NTLM Dance" - ie, the NTLM authentication handshake - has to be done at the lower level (at the http.client level) during tunnel creation. Use the HttpNtlmAdapter and requests session. This HttpNtlmAdapter is responsible for sending proxy auth information downstream.

Here is a basic example:

import requests
from requests_ntlm2.auth import HttpNtlmAuth
from requests_ntlm2.compatibility import NtlmCompatibility
from requests_ntlm2.proxy.adapters.ntlm import HttpNtlmAdapter

username = '...'
password = '...'
proxy_ip = '...'
proxy_port = '...'

proxies = {
    'http': f'http://{proxy_ip}:{proxy_port}',
    'https': f'http://{proxy_ip}:{proxy_port}'
}

ntlm_compatibility = NtlmCompatibility.NTLMv2_DEFAULT

session = requests.Session()
session.mount(
    'https://',
    HttpNtlmAdapter(
        username,
        password,
        ntlm_compatibility=ntlm_compatibility
    )
)
session.mount(
    'http://',
    HttpNtlmAdapter(
        username,
        password,
        ntlm_compatibility=ntlm_compatibility
    )
)
session.auth = HttpNtlmAuth(
    username,
    password,
    ntlm_compatibility=ntlm_compatibility
)
session.proxies = proxies

response = session.get('http:/foobar.com')

Requirements

Development

The package supports Python 3.6 and later. Install uv and just to run the development commands.

uv sync --locked --python 3.13
just lint
just test
just build

Run just ruff, just bandit, just typecheck, or just imports for an individual check. just test runs pytest with coverage, starts and stops the local test server, and enables OpenSSL's legacy provider for the NTLM tests when available. The test recipe uses Bash.

The separate functional suite requires Windows IIS. See tests/functional/setup_iis.ps1 for its setup.

Metadata

Release files for requests-ntlm2 7.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for requests-ntlm2 7.0.0
File Size Uploaded Provenance Actions
requests_ntlm2-7.0.0.tar.gz 32.9 kB Without provenance Details for requests_ntlm2-7.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for requests-ntlm2 7.0.0
File Interpreter ABI Platform Actions
requests_ntlm2-7.0.0-py3-none-any.whl Python 3 none any Details for requests_ntlm2-7.0.0-py3-none-any.whl

Total release size: 49.2 kB

Release files / requests_ntlm2-7.0.0.tar.gz

Download URL requests_ntlm2-7.0.0.tar.gz
Size 32.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8bc646440726b09c4568d58c95e00eea6ef8f86fa30da3454a9a99eeb2fd21d4
BLAKE2b-256 checksum
How to use checksums
09b01ee2d5f22fc5dee411920b0937f1e922a83150610d835be0f9f0a5ee79cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / requests_ntlm2-7.0.0-py3-none-any.whl

Download URL requests_ntlm2-7.0.0-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9c38e52f74b514b3e42560014fdfbbb7ce9227a4f0238dbd59351344142f6e7
BLAKE2b-256 checksum
How to use checksums
9400b1cc570890f522448e366ff3489bb21c6dba25cf95cf29c5b3a5d45f579b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for requests-ntlm2 7.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

7.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page