TLS Client
Python-TLS-Client is an advanced HTTP client library (version 1.0.1) designed to mimic browser-like TLS fingerprints and behavior, helping to bypass bot protection mechanisms. It offers a similar syntax to the popular `requests` library. The library maintains an active development pace, with frequent updates incorporating new features and shared library improvements.
Warnings
- gotcha Failing to explicitly call `session.close()` can lead to significant memory leaks, especially in long-running applications or when making many requests. Version 1.0.0 introduced the `close` function to address this.
- gotcha While `client_identifier` helps mimic TLS fingerprints, it may not automatically set the `User-Agent` header. For full browser-like behavior and to bypass some bot detection, manually specify a `User-Agent` header in your requests.
- gotcha Certificate pinning, added in version 1.0.1, requires pre-generating pins using a separate GoLang tool (`hpkp-pins`). Misconfiguration or failure to provide valid pins can lead to connection failures, as it rigorously validates server certificates.
- gotcha Older versions (pre-1.0.0) of `tls-client` had known bugs related to cookie handling (e.g., duplicate cookies, issues with auto-redirects). Using outdated versions might lead to unexpected behavior in stateful interactions. [cite: GitHub releases]
- gotcha Connections might fail if the underlying system's OpenSSL library or Python environment does not support modern TLS protocols (like TLS 1.2 or 1.3), as many servers deprecate older, insecure versions like TLS 1.0/1.1.
- gotcha Some users have reported memory or connection issues that could be mitigated by forcing HTTP/1.1 instead of HTTP/2. This may indicate specific server or environmental compatibility challenges with the underlying GoLang library's HTTP/2 implementation.
Install
-
pip install tls-client
Imports
- Session
import tls_client session = tls_client.Session()
Quickstart
import tls_client
import os
# Initialize a TLS client session mimicking Chrome 120
session = tls_client.Session(
client_identifier="chrome120",
random_tls_extension_order=True,
proxy=os.environ.get('TLS_PROXY', '') # Optional: load proxy from environment variable
)
try:
# Make a GET request with browser-like headers
response = session.get(
"https://www.example.com/",
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br"
},
timeout_seconds=30
)
print(f"Status Code: {response.status_code}")
print(f"Response Body (first 200 chars): {response.text[:200]}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
session.close() # Crucial for releasing resources and preventing memory leaks