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.

中文文档

Install

pip install --upgrade curl_cffi

This should work for Linux(x86_64/aarch64), macOS(Intel), 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)

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
  • 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) and Windows.
  • Support musllinux(alpine) and macOS(Apple Silicon) 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.

Change Log

  • 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.3.1.tar.gz (74.5 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.3.1-cp37-abi3-win_amd64.whl (2.5 MB view details)

Uploaded CPython 3.7+Windows x86-64

curl_cffi-0.3.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.3.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.3.1-cp37-abi3-macosx_11_0_arm64.whl (2.1 MB view details)

Uploaded CPython 3.7+macOS 11.0+ ARM64

curl_cffi-0.3.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.3.1.tar.gz.

File metadata

  • Download URL: curl_cffi-0.3.1.tar.gz
  • Upload date:
  • Size: 74.5 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.3.1.tar.gz
Algorithm Hash digest
SHA256 44e6934250f68bc6b28359947b07757517a215082abbe78685abe722ecc9c1f4
MD5 ad1f715470c60eea39c5d3af6a1ba5e3
BLAKE2b-256 7df23d2292e6cf79d6fb0f58c9bef1f6051c477ae7b005de2d55b4dae0b24c8a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: curl_cffi-0.3.1-cp37-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.5 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.3.1-cp37-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4b97cd481f2c48499c7281899493b0ed8a9d47ef3b02f50931c12d1f1f58c65d
MD5 6ef528c92b37920acba4ad90eafa6b6e
BLAKE2b-256 3c170e5860072048188f6562736c1560933fb3ea70be728f58c5d3c89600f91b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for curl_cffi-0.3.1-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f96b65da063c5cfb55630911d8958f08bab071ff3ff5b222b031b897afe004c
MD5 a9d1ef4d706216b111e74da30b5c4bf2
BLAKE2b-256 3147656f006c89d386cdfc08eaba172ef2babda89c6d5520dd4a6687f8da6bbf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for curl_cffi-0.3.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a851a924647456ea27e938fed4b08cf858f51e59a90d41d577f1b87304e258fe
MD5 cd1807eb53b5029646e216489207ab8c
BLAKE2b-256 f804bb1c834d88d0748db933650e4874c7554a6e654bd75f215c065a6bd197f2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for curl_cffi-0.3.1-cp37-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c679825b42686e3ea76daf3c5726a3b82a11a23cc28534825b799bd90d178b82
MD5 475c19f2849eeac637f077c1ef7aede1
BLAKE2b-256 016fc2e69fd95fb7dd99c257043852619e3f174cd7ef2758538e3c51b6fc9d0f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for curl_cffi-0.3.1-cp37-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 09105e59d2000e21b401c4278b0f000a8198ed2edf780154f0dfef246b1c55d4
MD5 93dbf06320ed926749e0a9ed887890db
BLAKE2b-256 7a7f6fdd2664d16a3c1ba7229a4c6e257a6990cbc9ae6a9cb2f7d957440e435b

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