Python API¶
Generated from the public tlsforge exports and their runtime signatures. See the Python integration guide for examples.
Client¶
One browser identity: one fingerprint, one cookie jar, one exit IP.
A client serialises: the protocol underneath is one request at a time, so calls from several threads queue rather than overlap. That is not a limitation to work around — it is what one session is. Scrape in parallel with a pool of clients, one per proxy, which is also how the identities stay separate.
Close it when done, or use it as a context manager. Closing is final: a client is one identity, and once it has been given up there is no identity left to make requests with.
Parameters
profilestr | Nonedefault None"chrome".proxystr | Nonedefault Nonehttp:// or socks5:// URL.timeoutfloatdefault DEFAULT_TIMEOUTbinarystr | os.PathLike[str] | Nonedefault Noneinsecurebooldefault Falsecookie_filestr | os.PathLike[str] | Nonedefault None--cookies).cookie_setstr | Nonedefault Noneon_stderrCallable[[str], None] | Nonedefault NoneRaises
Client(
*,
profile: str | None = None,
proxy: str | None = None,
timeout: float = DEFAULT_TIMEOUT,
binary: str | os.PathLike[str] | None = None,
insecure: bool = False,
cookie_file: str | os.PathLike[str] | None = None,
cookie_set: str | None = None,
on_stderr: Callable[[str], None] | None = None,
)
| Example | |
|---|---|
Client.get()¶
GET a URL.
Client.post()¶
POST a body, in the browser context of the form or script that posts it: a request without one is a navigation typed into the address bar, which posts nothing, and is refused.
Client.hello()¶
What the transport is: the protocol it speaks and what it can do, asked without a connection, and once. One older than the question speaks protocol 1, and can do nothing a client has to ask about.
Raises
Client.request()¶
Make a request.
Parameters
urlstrmethodstrdefault 'GET'headersMapping[str, str] | Nonedefault Noneuser-agent, by the bytes of its name; in one, what the page's script gives fetch() or an XHR.orderSequence[str] | Nonedefault Nonebodystr | bytes | Nonedefault NonecookiesIterable[str] | Nonedefault Nonename=value pairs added to the jar before the request.browser_contextBrowserContext | Nonedefault NoneBrowserContext), sent only to a transport that says, when asked with hello, that it applies one.network_contextNetworkContext | Nonedefault NoneRaises
TypeErrorClient.request(
url: str,
*,
method: str = 'GET',
headers: Mapping[str, str] | None = None,
order: Sequence[str] | None = None,
body: str | bytes | None = None,
cookies: Iterable[str] | None = None,
browser_context: BrowserContext | None = None,
network_context: NetworkContext | None = None,
) -> Response
| Example | |
|---|---|
Client.close()¶
Stop the transport. Idempotent, and final.
At once: a request in flight, or one waiting for it, is not waited for, and raises TransportError. A write stuck on a pipe the transport has stopped reading is ended by stopping the transport first.
BrowserContext¶
What in a browser a request stands for: it goes out as Chrome sends that kind, CORS and all. See GO.md, «Browser contexts».
The keys are this package's names for the daemon's fields, which the client sends as the daemon names them: referrer_policy is its referrerPolicy. A key there is none of is a TypeError.
Keys
kindLiteral['navigation', 'form', 'fetch', 'xhr']initiatorstroptionalreferrer_policyLiteral['no-referrer', 'no-referrer-when-downgrade', 'origin', 'origin-when-cross-origin', 'same-origin', 'strict-origin', 'strict-origin-when-cross-origin', 'unsafe-url']optionalmodeLiteral['cors', 'no-cors', 'same-origin']optionalcredentialsLiteral['same-origin', 'include', 'omit']optionalNetworkContext¶
Actual network metadata, with no precomputed RTT or downlink values.
Keys
typeLiteral['unknown', 'ethernet', 'wifi', '2g', '3g', '4g', '5g', 'none', 'bluetooth']idstrgenerationintsignalOptional[int]Hello¶
What a transport is, as it says when asked: the protocol it speaks, and what it can do that a client has to ask about before relying on it. One older than the question speaks protocol 1 and can do nothing it is asked about.
Fields
protocolintcapabilitiestuple[str, ...]engine_majorint | Nonedefault Noneversionstr | Nonedefault NoneResponse¶
What came back.
Fields
statusinturlstrbodystrcontent when the response is not text.contentbytesdefault b''headersMapping[str, tuple[str, ...]]default dict()set-cookie, among others, changes its meaning.cookiestuple[str, ...]default ()Response.ok¶
Did the server answer with a 2xx?
Response.json()¶
The body parsed as JSON. Raises json.JSONDecodeError if it is not.
DEFAULT_TIMEOUT¶
MAX_REQUEST_BYTES¶
The most a request can be as the transport reads it: one line of JSON, its newline included, in UTF-8. A longer one would end the transport, and the session in it, so it is refused before it is sent.
TLSForgeError¶
Base for everything this package raises.
BinaryNotFound¶
The transport binary could not be found or does not exist.
An install problem rather than a request problem: no retry will help, and the message says which of the four places were looked in.
TransportError¶
The transport process failed: it would not start, died, or spoke nonsense.
The request did not happen, or happened and the answer was lost. The client stays usable — the next request starts a fresh process.
SessionReset¶
The request was not sent: the transport the client's session was in has gone, and its cookies, TLS sessions and connections went with it.
Raised for every request waiting when that happens, or for the next one made when none was waiting. Requests after that start a new session in a new transport: the client never promises the identity it had before. A TransportError, so that code catching those catches this too.
RequestFailed¶
The transport ran the request and the request itself failed.
A refused connection, a DNS failure, a TLS error: the far end's doing, not the transport's. Distinct from TransportError because retrying this one can make sense and there is nothing wrong locally.
| Example | |
|---|---|
Timeout¶
The deadline passed with no answer.
Also a builtin TimeoutError, so code that already catches those catches this without knowing about this package.
Unsupported¶
The transport cannot make the request as asked, and it was not sent.
It does not do what the request needs, as it says when asked with Client.hello(), or it is older than the question. A newer tls-forge, or a request that does not need it, is the fix.
resolve_binary()¶
Find the transport binary.
Parameters
explicitstr | os.PathLike[str] | Nonedefault NoneRaises
| Example | |
|---|---|
bundled_binary()¶
The binary shipped inside this package, whether or not it is there.
exe_name()¶
The executable's name, which is the command's name, not the package's.
ENV_VAR¶
The environment variable that overrides every other source.