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
(manylinux_2_28|musllinux_1_2),aarch64
(manylinux_2_34‼️|musllinux_1_2); - 🪟 windows:
amd64
; - 🍏 macos:
amd64
,aarch64
.
Table of Contents
Installation
pip install -U primp
Features
- Impersonate: impersonate option mimics web browsers by replicating their headers and TLS/JA3/JA4/HTTP2 fingerprints.
- Automatic Character Encoding Detection: the encoding is taken from the
Content-Type
header or<meta ... charset=
within the html, or if not found,UTF-8
. If the encoding is incorrect, the library tries to determine the correct encoding to decode the text. - Html2text: the
text_plain
|text_markdown
|text_rich
response attributes extract plain|markdown|rich text from html. - All Decoders:
gzip
,brotli
,zstd
are already included and do not require third party packages. - High Performance: the library is designed for a large number of threads, uses all processors, and releases the GIL.
- Thread-safe: library can be safely used in multithreaded environments.
- Small Size: the compiled library is about 6.8MB in size.
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"
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"
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.url
Examples
import primp
client = primp.Client(impersonate="chrome_127")
# 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()
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())
# 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_127")
print(r.text)
II. AsyncClient
TODO
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
primp-0.6.0.tar.gz
(79.9 kB
view hashes)
Built Distributions
Close
Hashes for primp-0.6.0-cp38-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf391210e28397ddbbbea7216c8fe72caa61a3a407e7faba351825ecf6ff3bec |
|
MD5 | 85b00e864ad3ba87cd1ce54c59f24eaa |
|
BLAKE2b-256 | 8a420d37535267d69d3ad519071b734b694caacedbbdd3837a38d373fe333754 |
Close
Hashes for primp-0.6.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74585b3fb460dd13379b36d6c7ebc1efb24e92d21696476b93c4b97a2638f4ac |
|
MD5 | e6e86a98a2867928192aebcfb8be3f15 |
|
BLAKE2b-256 | 39a63385125b2082280acde46f8b50fd50916bc4c7c9a98c538ac23e706b3ffd |
Close
Hashes for primp-0.6.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e941355ca729eb277613f9c5ee0b0f4273d3df9440a177dccc1780ad0c752858 |
|
MD5 | 5d36d152190fd3c3989730256d6b9afb |
|
BLAKE2b-256 | 8ea4cce2b7aa0bcf1214a6395bee3545001ea969073fe0280fd64e78656bb9de |
Close
Hashes for primp-0.6.0-cp38-abi3-manylinux_2_34_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f36ff625e1531f051bdad68d4cd8832ba8c7f1dec7b430af8206562b37656a28 |
|
MD5 | b59ac1a7a768f37286deba61cb83e056 |
|
BLAKE2b-256 | 315e6b1d98da1e2d72a93ff47bce3036527f59255ab4fcc319285c3a8e89813f |
Close
Hashes for primp-0.6.0-cp38-abi3-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e16aaab98bddea6fef90337e170fd2c232d399fb75d36dda53dd9f956cd9eeb |
|
MD5 | 8dbfa44385258000454965d27cef6142 |
|
BLAKE2b-256 | aafd3eac9308e53d7df7ee1f4c3ebc594d861b29bb67813821ce9452f73a6e75 |
Close
Hashes for primp-0.6.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7c2f0443afd3064efbe267e0515657173531bc89ee36ea21794edf08d931be9 |
|
MD5 | d705da72399c5c478b9b5ceb97242de9 |
|
BLAKE2b-256 | acc9d70f2f9d4d88ef1c5e21c5f8e67cc344629e9dcabda2259eda04f3d32745 |
Close
Hashes for primp-0.6.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e74173d87b232d276b0a64ce21717b993e2460677cb81d3289965a80f6675abd |
|
MD5 | 7d90b56391e86eb10fa77a40b54d11f4 |
|
BLAKE2b-256 | 249ed19e744a174444cfe591f9a08ec5bc43b361163eb24f826d6c89cf675a96 |