HTTP client that can impersonate web browsers, mimicking their headers and `TLS/JA3/JA4/HTTP2` fingerprints
Project description
🪞PRIMP
🪞PRIMP = Python Requests IMPersonate
The fastest python HTTP client that can impersonate web browsers.
Provides precompiled wheels:
- 🐧 linux|musl:
amd64
,aarch64
(⚠️warning: linuxaarch64
build ismanylinux_2_34
compatible -ubuntu>=22.04
,debian>=12
); - 🪟 windows:
amd64
; - 🍏 macos:
amd64
,aarch64
.
Table of Contents
Installation
pip install -U primp
Benchmark
Usage
I. Client
HTTP client that can impersonate web browsers.
class Client:
"""Initializes an HTTP client that can impersonate web browsers.
Args:
auth (tuple, optional): A tuple containing the username and password for basic authentication. Default is None.
auth_bearer (str, optional): Bearer token for authentication. Default is None.
params (dict, optional): Default query parameters to include in all requests. Default is None.
headers (dict, optional): Default headers to send with requests. If `impersonate` is set, this will be ignored.
cookies (dict, optional): - An optional map of cookies to send with requests as the `Cookie` header.
timeout (float, optional): HTTP request timeout in seconds. Default is 30.
cookie_store (bool, optional): Enable a persistent cookie store. Received cookies will be preserved and included
in additional requests. Default is True.
referer (bool, optional): Enable or disable automatic setting of the `Referer` header. Default is True.
proxy (str, optional): Proxy URL for HTTP requests. Example: "socks5://127.0.0.1:9150". Default is None.
impersonate (str, optional): Entity to impersonate. Example: "chrome_124". Default is None.
Chrome: "chrome_100","chrome_101","chrome_104","chrome_105","chrome_106","chrome_107","chrome_108",
"chrome_109","chrome_114","chrome_116","chrome_117","chrome_118","chrome_119","chrome_120",
"chrome_123","chrome_124","chrome_126","chrome_127","chrome_128","chrome_129","chrome_130",
"chrome_131"
Safari: "safari_ios_16.5","safari_ios_17.2","safari_ios_17.4.1","safari_15.3","safari_15.5","safari_15.6.1",
"safari_16","safari_16.5","safari_17.0","safari_17.2.1","safari_17.4.1","safari_17.5","safari_18",
"safari_ipad_18"
OkHttp: "okhttp_3.9","okhttp_3.11","okhttp_3.13","okhttp_3.14","okhttp_4.9","okhttp_4.10","okhttp_5"
Edge: "edge_101","edge_122","edge_127"
follow_redirects (bool, optional): Whether to follow redirects. Default is True.
max_redirects (int, optional): Maximum redirects to follow. Default 20. Applies if `follow_redirects` is True.
verify (bool, optional): Verify SSL certificates. Default is True.
ca_cert_file (str, optional): Path to CA certificate store. Default is None.
http1 (bool, optional): Use only HTTP/1.1. Default is None.
http2 (bool, optional): Use only HTTP/2. Default is None.
"""
Client methods
The Client
class provides a set of methods for making HTTP requests: get
, head
, options
, delete
, post
, put
, patch
, each of which internally utilizes the request()
method for execution. The parameters for these methods closely resemble those in httpx
.
def get(
url: str,
params: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
cookies: Optional[Dict[str, str]] = None,
auth: Optional[Tuple[str, Optional[str]]] = None,
auth_bearer: Optional[str] = None,
timeout: Optional[float] = 30,
):
"""Performs a GET request to the specified URL.
Args:
url (str): The URL to which the request will be made.
params (Optional[Dict[str, str]]): A map of query parameters to append to the URL. Default is None.
headers (Optional[Dict[str, str]]): A map of HTTP headers to send with the request. Default is None.
cookies (Optional[Dict[str, str]]): - An optional map of cookies to send with requests as the `Cookie` header.
auth (Optional[Tuple[str, Optional[str]]]): A tuple containing the username and an optional password
for basic authentication. Default is None.
auth_bearer (Optional[str]): A string representing the bearer token for bearer token authentication. Default is None.
timeout (Optional[float]): The timeout for the request in seconds. Default is 30.
"""
def post(
url: str,
params: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str, str]] = None,
cookies: Optional[Dict[str, str]] = None,
content: Optional[bytes] = None,
data: Optional[Dict[str, str]] = None,
json: Any = None,
files: Optional[Dict[str, bytes]] = None,
auth: Optional[Tuple[str, Optional[str]]] = None,
auth_bearer: Optional[str] = None,
timeout: Optional[float] = 30,
):
"""Performs a POST request to the specified URL.
Args:
url (str): The URL to which the request will be made.
params (Optional[Dict[str, str]]): A map of query parameters to append to the URL. Default is None.
headers (Optional[Dict[str, str]]): A map of HTTP headers to send with the request. Default is None.
cookies (Optional[Dict[str, str]]): - An optional map of cookies to send with requests as the `Cookie` header.
content (Optional[bytes]): The content to send in the request body as bytes. Default is None.
data (Optional[Dict[str, str]]): The form data to send in the request body. Default is None.
json (Any): A JSON serializable object to send in the request body. Default is None.
files (Optional[Dict[str, bytes]]): A map of file fields to file contents to be sent as multipart/form-data. Default is None.
auth (Optional[Tuple[str, Optional[str]]]): A tuple containing the username and an optional password
for basic authentication. Default is None.
auth_bearer (Optional[str]): A string representing the bearer token for bearer token authentication. Default is None.
timeout (Optional[float]): The timeout for the request in seconds. Default is 30.
"""
Response object
resp.content
resp.cookies
resp.encoding
resp.headers
resp.json()
resp.status_code
resp.text
resp.text_markdown # html is converted to markdown text
resp.text_plain # html is converted to plain text
resp.text_rich # html is converted to rich text
resp.url
Examples
import primp
client = primp.Client(impersonate="chrome_131")
# GET request
resp = client.get("https://tls.peet.ws/api/all")
print(resp.json())
# GET request with passing params and setting timeout
params = {"param1": "value1", "param2": "value2"}
resp = client.post(url="https://httpbin.org/anything", params=params, timeout=10)
print(r.text)
# POST Binary Request Data
content = b"some_data"
resp = client.post(url="https://httpbin.org/anything", content=content)
print(r.text)
# POST Form Encoded Data
data = {"key1": "value1", "key2": "value2"}
resp = client.post(url="https://httpbin.org/anything", data=data)
print(r.text)
# POST JSON Encoded Data
json = {"key1": "value1", "key2": "value2"}
resp = client.post(url="https://httpbin.org/anything", json=json)
print(r.text)
# POST Multipart-Encoded Files
files = {'file1': open('file1.txt', 'rb').read(), 'file2': open('file2.txt', 'rb').read()}
r = client.post("https://httpbin.org/post", files=files)
print(r.text)
# Authentication using user/password
auth = ("user", "password")
resp = client.post(url="https://httpbin.org/anything", auth=auth)
print(r.text)
# Authentication using auth bearer
auth_bearer = "bearerXXXXXXXXXXXXXXXXXXXX"
resp = client.post(url="https://httpbin.org/anything", auth_bearer=auth_bearer)
print(r.text)
# Using proxy
resp = primp.Client(proxy="http://127.0.0.1:8080").get("https://tls.peet.ws/api/all")
print(resp.json())
# Using custom CA certificate store: file or certifi.where() or env var CA_CERT_FILE
resp = primp.Client(ca_cert_file="/cert/cacert.pem").get("https://tls.peet.ws/api/all")
print(resp.json())
resp = primp.Client(ca_cert_file=certifi.where()).get("https://tls.peet.ws/api/all")
print(resp.json())
export CA_CERT_FILE="/home/user/Downloads/cert.pem"
resp = primp.Client().get("https://tls.peet.ws/api/all")
print(resp.json())
# You can also use convenience functions that use a default Client instance under the hood:
# primp.get() | primp.head() | primp.options() | primp.delete() | primp.post() | primp.patch() | primp.put()
# These functions can accept the `impersonate` parameter:
resp = primp.get("https://httpbin.org/anything", impersonate="chrome_131")
print(r.text)
II. AsyncClient
TODO
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file primp-0.8.0.tar.gz
.
File metadata
- Download URL: primp-0.8.0.tar.gz
- Upload date:
- Size: 80.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6472651b8270247b3121f728b613e312301b8f7e9170944a4e71771dd58eaa8b |
|
MD5 | 0ffbe96003635f503f71686213973012 |
|
BLAKE2b-256 | 8abc1ac40937ea38fda4b0b97385122563e4457c4f383e451138882988783973 |
File details
Details for the file primp-0.8.0-cp38-abi3-win_amd64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 2.9 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcf9895f8dd97d49843adbed635d713e3a1c2dc0a4b08ac0879292be83f1e447 |
|
MD5 | c5729f182786fb89d68f71f13ccd6132 |
|
BLAKE2b-256 | b20f72100c6454c3a1b01d710bce9be80757a85726f2f8283f955e0fe38867a6 |
File details
Details for the file primp-0.8.0-cp38-abi3-musllinux_1_2_x86_64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 3.2 MB
- Tags: CPython 3.8+, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea18ebc1f664898beac62cfa092ff38ad70c7eb0b3120aecd18ab7a776b6b3fb |
|
MD5 | 0e04226f0dd96004a857985617b278e8 |
|
BLAKE2b-256 | f787c0f5a36551cde488c05a899cf1a15395e376e1d1d08331e2425c44ffd14d |
File details
Details for the file primp-0.8.0-cp38-abi3-musllinux_1_2_aarch64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-musllinux_1_2_aarch64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.8+, musllinux: musl 1.2+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6672554a653f4ef5e672f3985481bc4afff9bfbeaf2bc7b70b9230b7672d49d6 |
|
MD5 | de701d1db3e95f54f318cbb552e89b63 |
|
BLAKE2b-256 | 024a743d4f9e0adab5b69e78c90fb32a4c464b4050205db63f121e16c722b53f |
File details
Details for the file primp-0.8.0-cp38-abi3-manylinux_2_34_aarch64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-manylinux_2_34_aarch64.whl
- Upload date:
- Size: 2.9 MB
- Tags: CPython 3.8+, manylinux: glibc 2.34+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e341c821fa265f2eaf2a0de80924e465f7bc20a84e9ce28e65cee350ad2cc300 |
|
MD5 | 5dae9da8150fc25062a465d20016161c |
|
BLAKE2b-256 | 377bf5ed62b79d84199a80dd3f19835acd2f505930294261bc296c45df90f404 |
File details
Details for the file primp-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 3.0 MB
- Tags: CPython 3.8+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 609f4363fb591bde351e6372ba0caaf1ac963d38cbf942bc42dc3284575b4cdf |
|
MD5 | adf01fce7af3b566eb48bc800132823b |
|
BLAKE2b-256 | a60b56fe79c313592d13e685c839886dbeac1f8cad4b6a39e0bc5db4fbf4c9df |
File details
Details for the file primp-0.8.0-cp38-abi3-macosx_11_0_arm64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 2.7 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 135e6350a6c509fcc3d1cc03d2025edd54783bca671a39a2d4f240ce5d406576 |
|
MD5 | 8d9e2c53f2495a5841aff3fec6a1f062 |
|
BLAKE2b-256 | 571ea3eabec19dd5dbc89f6453ce2c2ecdbaeeb7ee00fcf7441abfbe6628e537 |
File details
Details for the file primp-0.8.0-cp38-abi3-macosx_10_12_x86_64.whl
.
File metadata
- Download URL: primp-0.8.0-cp38-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 2.9 MB
- Tags: CPython 3.8+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cb4d1db83d92a95fb4506d4605484b389a988fb962e80089caa73c035185f58 |
|
MD5 | ed295be2988296125fbbe986ba2be0e0 |
|
BLAKE2b-256 | cf54dd0b454302d94c480ffbcfc170ed363ac8f2aaba280e43230f278e99f18a |