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: amd64, aarch64, armv7 (⚠️aarch64 and armv7 builds are manylinux_2_34 compatible - ubuntu>=22.04, debian>=12);
  • 🐧 musllinux: amd64, aarch64;
  • 🪟 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


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.8.1.tar.gz (80.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

primp-0.8.1-cp38-abi3-win_amd64.whl (2.9 MB view details)

Uploaded CPython 3.8+Windows x86-64

primp-0.8.1-cp38-abi3-musllinux_1_2_x86_64.whl (3.2 MB view details)

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

primp-0.8.1-cp38-abi3-musllinux_1_2_aarch64.whl (3.1 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

primp-0.8.1-cp38-abi3-manylinux_2_34_armv7l.whl (2.7 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ ARMv7l

primp-0.8.1-cp38-abi3-manylinux_2_34_aarch64.whl (2.9 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ ARM64

primp-0.8.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view details)

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

primp-0.8.1-cp38-abi3-macosx_11_0_arm64.whl (2.7 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

primp-0.8.1-cp38-abi3-macosx_10_12_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for primp-0.8.1.tar.gz
Algorithm Hash digest
SHA256 ddf05754a7b70d59df8a014a8585e418f9c04e0b69065bab6633f4a9b92bad93
MD5 a3dec50228465fe940cb01e61a59175a
BLAKE2b-256 2b036c19a1b1f52d89b1337b082f2443faa78d660a576fad894ac6fd5ef35501

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primp-0.8.1-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

Hashes for primp-0.8.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6f0018a26be787431504e32548b296a278abbe85da43bcbaf2d4982ac3dcd332
MD5 a4912a124091935715bac1d9bd061f8d
BLAKE2b-256 53962ce2345ddd3eb9db57ac1f23803fd28c6602e5c115e4229f050d77d36f9b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 98f7f3a9481c55c56e7eff9024f29e16379a87d5b0a1b683e145dd8fcbdcc46b
MD5 4d2265c000c89c943cdf3161864a64a5
BLAKE2b-256 6501c0df6d0c93c7fde6dc0216f616f5054c1406b3d3ef49da256a1e0159d4c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 92f5f8267216252cfb27f2149811e14682bb64f0c5d37f00d218d1592e02f0b9
MD5 9296b04d5f2d12f833d53393091ffcc4
BLAKE2b-256 d6f437ab0b1ca98e176d8dc5ebfc24c0c314a140be73a632089f0adeaa1bcf42

See more details on using hashes here.

File details

Details for the file primp-0.8.1-cp38-abi3-manylinux_2_34_armv7l.whl.

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-manylinux_2_34_armv7l.whl
Algorithm Hash digest
SHA256 e8483b8d9eec9fc43d77bb448555466030f29cdd99d9375eb75155e9f832e5bd
MD5 16bac0467c8cc07cce5058973f0fd24c
BLAKE2b-256 0f17fc5237e5e7560131358c03cd078ea34e5cd4ae5bb402170029200b8e7d5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 4a27ac642be5c616fc5f139a5ad391dcd0c5964ace56fe6cf31cbffb972a7480
MD5 f1a5272594a9d896ae57a58e7bdac884
BLAKE2b-256 e18593258249b124aacf818db131902da6228525a554f52ff4894a5592f9df1c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 993cc4284e8c5c858254748f078e872ba250c9339d64398dc000a8f9cffadda3
MD5 e9653f6cf17bde09df8a71e0e0307273
BLAKE2b-256 5e671c0910efb5a22dea57a01d7e05a40a70319262e8c717bebd18eaaf9b2dac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e8117531dcdb0dbcf9855fdbac73febdde5967ca0332a2c05b5961d2fbcfe749
MD5 f74c22e5b312640922c5a6c999d76340
BLAKE2b-256 f663bfddbd5bef665869ddff9606190d01cc25a2825dec1bf5097e5635642eb8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for primp-0.8.1-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8294db817701ad76b6a186c16e22cc49d36fac5986647a83657ad4a58ddeee42
MD5 5dd1b83e6b3db5396df9d298af62276d
BLAKE2b-256 ad574f309cc05fff3ddb99bf564b332b19a61286b7145ae3d523e17afcdfc065

See more details on using hashes here.

Supported by

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