requests-ntlm2
requests-ntlm2, which is based on requests-ntlm, allows for HTTP NTLM authentication using the requests library.
Installation
pip install requests-ntlm2
Usage
Basic Usage
HttpNtlmAuth extends requests AuthBase, so usage is simple:
import requests
from requests_ntlm2 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 import HttpNtlmAuth, 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 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 httplib level) at tunnel-creation step. This means that you should 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 import (
HttpNtlmAuth,
HttpNtlmAdapter,
NtlmCompatibility
)
username = '...'
password = '...'
proxy_ip = '...'
proxy_port = '...'
proxies = {
'http': 'http://{}:{}'.format(proxy_ip, proxy_port),
'https': 'http://{}:{}'.format(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
Metadata
Release files for requests-ntlm2 6.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| requests_ntlm2-6.6.0.tar.gz | 14.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| requests_ntlm2-6.6.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.3 kB
Release files / requests_ntlm2-6.6.0.tar.gz
| Download URL | requests_ntlm2-6.6.0.tar.gz |
|---|---|
| Size | 14.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
85485a5c245e2e667bc0d01cb0baa46a96e1b55396a360bdda3b3219a95f6dc5
|
|
BLAKE2b-256 checksum How to use checksums |
b48b740255b889948a5cd136b665c81261474930a847748eb31cb9e978081287
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Release files / requests_ntlm2-6.6.0-py3-none-any.whl
| Download URL | requests_ntlm2-6.6.0-py3-none-any.whl |
|---|---|
| Size | 14.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
090d92de1ca9f5e6ebebb11e362628f109bcaa34bae85d3d2b87158e8583eead
|
|
BLAKE2b-256 checksum How to use checksums |
cb9ba27f2ad08ce908bdcc85851727ddbf7653c6c52edacf65f220a7cad42d62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|