Skip to main content

libcurl ffi bindings for Python, with impersonation support

Project description

curl_cffi

Python binding for curl-impersonate via CFFI.

中文文档

Unlike other pure python http clients like httpx or requests, this package can impersonate browsers' TLS signatures or JA3 fingerprints. If you are blocked by some website for no obvious reason, you can give this package a try.

Note on Chrome 110+ JA3 fingerprints

Chrome introduces ClientHello permutation in version 110, which means the order of extensions will be random, thus JA3 fingerprints will be random. So, when comparing JA3 fingerprints of curl_cffi and a browser, they may differ. However, this does not mean that TLS fingerprints will not be a problem, ClientHello extension order is just one factor of how servers can tell automated requests from browsers.

See more from this article and curl-impersonate notes

Install

pip install --upgrade curl_cffi

This should work for Linux(x86_64/aarch64), macOS(Intel/Apple Silicon), Windows(amd64). If it does not work, you may need to compile and install curl-impersonate first.

Usage

requests/httpx-like API:

from curl_cffi import requests

# Notice the impersonate parameter
r = requests.get("https://tls.browserleaks.com/json", impersonate="chrome101")

print(r.json())
# output: {'ja3_hash': '53ff64ddf993ca882b70e1c82af5da49'
# the fingerprint should be the same as target browser

# proxies are supported
proxies = {"https": "http://localhost:3128"}
r = requests.get("https://tls.browserleaks.com/json", impersonate="chrome101", proxies=proxies)

# socks proxies are also supported
proxies = {"https": "socks://localhost:3128"}
r = requests.get("https://tls.browserleaks.com/json", impersonate="chrome101", proxies=proxies)

Sessions

# sessions are supported
s = requests.Session()
# httpbin is a http test website
s.get("https://httpbin.org/cookies/set/foo/bar")
print(s.cookies)
# <Cookies[<Cookie foo=bar for httpbin.org />]>
r = s.get("https://httpbin.org/cookies")
print(r.json())
# {'cookies': {'foo': 'bar'}}

Supported impersonate versions:

  • chrome99
  • chrome100
  • chrome101
  • chrome104
  • chrome107
  • chrome110
  • chrome99_android
  • edge99
  • edge101
  • safari15_3
  • safari15_5

Alternatively, you can use the low-level curl-like API:

from curl_cffi import Curl, CurlOpt
from io import BytesIO

buffer = BytesIO()
c = Curl()
c.setopt(CurlOpt.URL, b'https://tls.browserleaks.com/json')
c.setopt(CurlOpt.WRITEDATA, buffer)

c.impersonate("chrome101")

c.perform()
c.close()
body = buffer.getvalue()
print(body.decode())

See example.py or tests/ for more examples.

API

Requests: almost the same as requests.

Curl object:

  • setopt(CurlOpt, value): Sets curl options as in curl_easy_setopt
  • perform(): Performs curl request, as in curl_easy_perform
  • getinfo(CurlInfo): Gets information in response after curl perform, as in curl_easy_getinfo
  • close(): Closes and cleans up the curl object, as in curl_easy_cleanup

Enum values to be used with setopt and getinfo, and can be accessed from CurlOpt and CurlInfo.

Trouble Shooting

Pyinstaller ModuleNotFoundError: No module named '_cffi_backend'

You need to tell pyinstaller to pack cffi and data files inside the package:

pyinstaller -F .\example.py --hidden-import=_cffi_backend --collect-all curl_cffi

Using https proxy, error: OPENSSL_internal:WRONG_VERSION_NUMBER

You are messing up https-over-http proxy and https-over-https proxy, for most cases, you should change {"https": "https://localhost:3128"} to {"https": "http://localhost:3128"}. Note the protocol in the url for https proxy is http not https.

See this issue for a detailed explaination.

Current Status

This implementation is very hacky now, but it works for most common systems.

When people installing other python curl bindings, like pycurl, they often face compiling issues or OpenSSL issues, so I really hope that this package can be distributed as a compiled binary package, uses would be able to use it by a simple pip install, no more compile errors.

For now, I just download the pre-compiled libcurl-impersonate from github and build a bdist wheel, which is a binary package format used by PyPI, and upload it. However, the right way is to download curl and curl-impersonate sources on our side and compile them all together.

Help wanted!

TODOs:

  • Write docs.
  • Binary package for macOS(Intel/AppleSilicon) and Windows.
  • Support musllinux(alpine) bdist by building from source.
  • Exclude the curl headers from source, download them when building.
  • Update curl header files and constants via scripts.
  • Implement requests.Session/httpx.Client.
  • Create ABI3 wheels to reduce package size and build time.
  • Set default headers as in curl-impersonate wrapper scripts.
  • Support stream in asyncio mode

Change Log

  • 0.5.0

    • Added asyncio support
  • 0.4.0

    • Removed c shim callback function, use cffi native callback function
  • 0.3.6

    • Updated to curl-impersonate v0.5.4, supported chrome107 and chrome110
  • 0.3.0, copied more code from httpx to support session

    • Add requests.Session
    • Breaking change: Response.cookies changed from http.cookies.SimpleCookie to curl_cffi.requests.Cookies
    • Using ABI3 wheels to reduce package size.

Acknowledgement

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

curl_cffi-0.5.1.tar.gz (27.0 kB view details)

Uploaded Source

Built Distributions

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

curl_cffi-0.5.1-cp37-abi3-win_amd64.whl (2.6 MB view details)

Uploaded CPython 3.7+Windows x86-64

curl_cffi-0.5.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view details)

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

curl_cffi-0.5.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.9 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.17+ ARM64

curl_cffi-0.5.1-cp37-abi3-macosx_11_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

curl_cffi-0.5.1-cp37-abi3-macosx_10_9_x86_64.whl (4.3 MB view details)

Uploaded CPython 3.7+macOS 10.9+ x86-64

File details

Details for the file curl_cffi-0.5.1.tar.gz.

File metadata

  • Download URL: curl_cffi-0.5.1.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for curl_cffi-0.5.1.tar.gz
Algorithm Hash digest
SHA256 680e53826c46fb920fca9d09a2b02412fb9b8fb66f706a699e0b6558181c8f64
MD5 7b96d260d612c701c73ea0c56a624aa4
BLAKE2b-256 81830130d3ff53a83e9f7e1043864c2a0b04da0314a6934e903d8016fb6cefef

See more details on using hashes here.

File details

Details for the file curl_cffi-0.5.1-cp37-abi3-win_amd64.whl.

File metadata

  • Download URL: curl_cffi-0.5.1-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.7+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for curl_cffi-0.5.1-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 2b6e3d448b671691e4b17aef2bb363e51ae5da5a7486fd791b681e3ec0af0fef
MD5 3fda11f7d030f344a0ce1fa4c59a765f
BLAKE2b-256 1072a4f16c8e2dd6a0b89368419a8e210e5688f2b1cbdc5742a4ec5b257d3811

See more details on using hashes here.

File details

Details for the file curl_cffi-0.5.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for curl_cffi-0.5.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 42af180aed300796ad79aadd86e7dde9b6dddf1c16cc857af0391ccdcf5e8de4
MD5 e9c68ab72c93971257bf6f7359fe1cf7
BLAKE2b-256 6c4ea7809bdd6f00445d8a5db7a062f7df58e628674574d86085d2bdda3b3dfd

See more details on using hashes here.

File details

Details for the file curl_cffi-0.5.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for curl_cffi-0.5.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 88b6efe98df6798d814ff99d7fd3b9193eb274725b6fcce1d1e52d4a109d6037
MD5 9db92e1219511616655c3040354aa5e4
BLAKE2b-256 458260ab4f69cdbcf10a7e6f01a1b143d9e64c1bc729e271a8a11b05b8724601

See more details on using hashes here.

File details

Details for the file curl_cffi-0.5.1-cp37-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for curl_cffi-0.5.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0c78159419639ebb6ce658788b8ae5ad2faaf79a83282b709d4b2baa7ce9efca
MD5 cb438e6d399ab4f4948d420b6e60c9d4
BLAKE2b-256 d9d90c56b111c84f54c8e8904d4b000517a295dc17878bb755731eb06587d03c

See more details on using hashes here.

File details

Details for the file curl_cffi-0.5.1-cp37-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for curl_cffi-0.5.1-cp37-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e52a596e586e84be52f72282af7af6c9c28ac89b9060107115c7d60cd7543857
MD5 2dc7cf1c4520ccd6345242acedbc3769
BLAKE2b-256 bca60fb7cc8d864762188c7e06d2470c9bf4f71ec71876d51b748a5d85c19634

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