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 most Linux systems. 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

Or, 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/ from 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 can be accessed from CurlOpt and CurlInfo.

Current Status

This repo is very early stage for now, the pypi package only works on Linux, not macOS or Windows. If you wish to use it on macOS or Windows, you need to clone it and the curl-impersonate repo, and then compile them by yourself.

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 and Windows.
  • Support musllinux(alpine) bdist by building curl-impersonate from source.
  • Update curl header files and constants via scripts.
  • Implement requests.Session/httpx.Session.

Acknowledgement

This package was originally forked from https://github.com/multippt/python_curl_cffi

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

curl_cffi-0.2.1.tar.gz (69.8 kB view hashes)

Uploaded Source

Built Distributions

curl_cffi-0.2.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

curl_cffi-0.2.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

curl_cffi-0.2.1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.8 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

curl_cffi-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.9 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-cp311-cp311-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

curl_cffi-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.9 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-cp310-cp310-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

curl_cffi-0.2.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.9 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

curl_cffi-0.2.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

curl_cffi-0.2.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.9 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

curl_cffi-0.2.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.2 MB view hashes)

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

curl_cffi-0.2.1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (6.9 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

curl_cffi-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

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