Skip to main content

HTTP client that can impersonate web browsers, mimicking their headers and `TLS/JA3/JA4/HTTP2` fingerprints

Project description

Python >= 3.8 Downloads CI

🪞PRIMP

🪞PRIMP = Python Requests IMPersonate

The fastest python HTTP client that can impersonate web browsers.
Provides precompiled wheels:

  • 🐧 linux|musl: amd64, aarch64 (⚠️warning: linux aarch64 build is manylinux_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"
            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_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.3.tar.gz (78.7 kB view details)

Uploaded Source

Built Distributions

primp-0.6.3-cp38-abi3-win_amd64.whl (2.8 MB view details)

Uploaded CPython 3.8+ Windows x86-64

primp-0.6.3-cp38-abi3-musllinux_1_2_x86_64.whl (3.1 MB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ x86-64

primp-0.6.3-cp38-abi3-musllinux_1_2_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.8+ musllinux: musl 1.2+ ARM64

primp-0.6.3-cp38-abi3-manylinux_2_34_aarch64.whl (2.7 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.34+ ARM64

primp-0.6.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ x86-64

primp-0.6.3-cp38-abi3-macosx_11_0_arm64.whl (2.5 MB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

primp-0.6.3-cp38-abi3-macosx_10_12_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.8+ macOS 10.12+ x86-64

File details

Details for the file primp-0.6.3.tar.gz.

File metadata

  • Download URL: primp-0.6.3.tar.gz
  • Upload date:
  • Size: 78.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.1

File hashes

Hashes for primp-0.6.3.tar.gz
Algorithm Hash digest
SHA256 17d30ebe26864defad5232dbbe1372e80483940012356e1f68846bb182282039
MD5 ea0158e6f6a274135ef8d1f530645b91
BLAKE2b-256 639c10d2c7b734228021cf17d92b2872ed53535103d29a38a6ad4eee89a8ae1b

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: primp-0.6.3-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.7.1

File hashes

Hashes for primp-0.6.3-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1e4113c34b86c676ae321af185f03a372caef3ee009f1682c2d62e30ec87348c
MD5 275f51a17970ada5871683fcf73fea35
BLAKE2b-256 98de5c1dab24c1bff7933d0e4e8f4d0f46b66fc23531258d8433789c4468cac3

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for primp-0.6.3-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7732bec917e2d3c48a31cdb92e1250f4ad6203a1aa4f802bd9abd84f2286a1e0
MD5 b25d569273242767fb814b1b6f403419
BLAKE2b-256 0955c96cb510c9f7881fa01bc7b269e446d32635ab9f0adbd36918c69b45a140

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for primp-0.6.3-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 d3cd9a22b97f3eae42b2a5fb99f00480daf4cd6d9b139e05b0ffb03f7cc037f3
MD5 0f05eb33d7d1f4a4a614aa635f9505f4
BLAKE2b-256 74a1a626fb8d2f6499d3e05971b2dcd33d770244722ee52f7d2c4ab636c1157f

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for primp-0.6.3-cp38-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 86337b44deecdac752bd8112909987fc9fa9b894f30191c80a164dc8f895da53
MD5 68aedaf932a9899a6a73bf854db87d2a
BLAKE2b-256 3a45ff51ccc5dbc82afa6d2dda15bf07b21b6b44708d02934cd7562007d1f719

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for primp-0.6.3-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 78da53d3c92a8e3f05bd3286ac76c291f1b6fe5e08ea63b7ba92b0f9141800bb
MD5 1f9b74f8967487cf945d216f0d4ab962
BLAKE2b-256 f785ca027d7ec6121346d5927205777ad74bf918f291a538d710b5f9e0957333

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for primp-0.6.3-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eeb53eb987bdcbcd85740633470255cab887d921df713ffa12a36a13366c9cdb
MD5 42b67b0f01e9b5aa5f4171dc924775d8
BLAKE2b-256 b3a1b58ac752b0500208df1be3b762eeaff1a117ec3108e26b38821ae9ac31e0

See more details on using hashes here.

File details

Details for the file primp-0.6.3-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for primp-0.6.3-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bdbe6a7cdaaf5c9ed863432a941f4a75bd4c6ff626cbc8d32fc232793c70ba06
MD5 133de0feb1110813e9cedddbcb7885ab
BLAKE2b-256 610996e8327fd8c1224226d9a170daf1dcba7d3f6578edeb9f811803f2a61aba

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page