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.constants
HttpProxyAdapter requests_ntlm2.proxy.adapters.base
HttpNtlmAdapter requests_ntlm2.proxy.adapters.ntlm
HTTPConnection, HTTPSConnection, VerifiedHTTPSConnection requests_ntlm2.proxy.connection

The original requests_ntlm2.compatibility.NtlmCompatibility import path also works.

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.constants 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.constants 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.1.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.1.0
File Size Uploaded Provenance Actions
requests_ntlm2-7.1.0.tar.gz 34.3 kB Without provenance Details for requests_ntlm2-7.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 52.2 kB

Release files / requests_ntlm2-7.1.0.tar.gz

Download URL requests_ntlm2-7.1.0.tar.gz
Size 34.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b6ac9532df354d5a641532c563eb7e51e65eec71e7156f3ec6eab50f23acaaa9
BLAKE2b-256 checksum
How to use checksums
e417fe6e79839d45917d8e9f9034cccddd62a03fcadb37ff784c363427fd0aee
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.1.0-py3-none-any.whl

Download URL requests_ntlm2-7.1.0-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5c76e38ebcb8f24fb4ed6f75d58ce3780060881c75466a7b5be462262afd274
BLAKE2b-256 checksum
How to use checksums
e92a3838def3d214224005eea0e12bcf34f35f1083dd18ba4ed7b666757e46dd
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.1.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.1.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