A requests.Session that proxies through a FlareSolverr instance.
Project description
FlareSolverr Session
A requests.Session that transparently routes all HTTP requests through a FlareSolverr instance, allowing you to bypass Cloudflare protection with a familiar Python API.
The package also provides a more powerful Adapter to handle complex requests if the Session is not sufficient.
The project ships with a command-line interface (CLI) for requests and session management, and an RPC client for direct access to the FlareSolverr JSON API.
This project is not responsible for solving challenges itself, it only forwards requests to FlareSolverr. If FlareSolverr fails to solve a challenge, it will raise an exception. Any issues related to challenge solving should be reported to the FlareSolverr project.
Installation
pip install flaresolverr-session
or
pip install flaresolverr-cli
Prerequisites
You need a running FlareSolverr instance. The quickest way is via Docker:
docker run -d --name=flaresolverr -p 8191:8191 ghcr.io/flaresolverr/flaresolverr:latest
Usage
Basic Usage
from flaresolverr_session import Session
with Session("http://localhost:8191/v1") as session:
response = session.get("https://example.com")
print(response.status_code)
print(response.text)
It is recommended to set a persistent session_id.
session = Session(
"http://localhost:8191/v1",
session_id="my-persistent-session",
)
Response Object
A FlareSolverr metadata object is attached to the response as response.flaresolverr. It contains details about the request and the challenge solving process returned by FlareSolverr.
| Attribute | Description |
|---|---|
flaresolverr.status |
"ok" on success |
flaresolverr.message |
Message from FlareSolverr (e.g. challenge status) |
flaresolverr.user_agent |
User-Agent used by FlareSolverr's browser |
flaresolverr.start / flaresolverr.end |
Request timestamps (ms) |
flaresolverr.version |
FlareSolverr server version |
Exception Handling
If FlareSolverr returns an error response, the session will raise a FlareSolverrResponseError exception.
All exceptions defined in the module are based on FlareSolverrError, which inherits from requests.RequestException. The hierarchy is as follows:
requests.RequestException
└── FlareSolverrError
├── FlareSolverrResponseError
│ └── FlareSolverrChallengeError
└── FlareSolverrUnsupportedMethodError
Exception Details:
| Exception | Description |
|---|---|
FlareSolverrResponseError |
FlareSolverr returned an error response. The response dict is available as response_data attribute. |
FlareSolverrChallengeError |
Challenge solving failed, raised only in Session. |
FlareSolverrUnsupportedMethodError |
Unsupported HTTP method or content type. |
Limitations
- Only GET and
application/x-www-form-urlencodedPOST are supported. Otherwise, it will raiseFlareSolverrUnsupportedMethodError. - Headers returned by FlareSolverr may be empty for some sites, depending on the FlareSolverr version and configuration. An empty HTTP status will be treated as
200. See FlareSolverr#1162.
If you need more control over the requests or want to use unsupported methods/content types, consider using the Adapter instead.
Command-Line Interface
After installation, you can use the flaresolverr-cli command. It is a convenient CLI tool to send HTTP requests through FlareSolverr and manage sessions.
It prints the json response from FlareSolverr. If the FlareSolverr URL is not provided via -f, it will use the FLARESOLVERR_URL environment variable (defaulting to http://localhost:8191/v1).
Sending requests
The request command is the default — you can omit the word request:
flaresolverr-cli https://example.com -o output.html
# GET with a custom FlareSolverr URL
flaresolverr-cli -f http://localhost:8191/v1 https://example.com
# POST with form data (data implies POST)
flaresolverr-cli https://example.com -d "key=value&foo=bar"
Managing sessions
# Create a session
flaresolverr-cli -f http://localhost:8191/v1 session create my-session
# Create multiple sessions at once
flaresolverr-cli session create session1 session2 session3
# List all active sessions
flaresolverr-cli session list
# Destroy a session
flaresolverr-cli session destroy my-session
# Clear all sessions
flaresolverr-cli session clear
Adapter
If your requests are more complex than standard GET or form POST, the module provides an adapter to retrieve Cloudflare challenge solutions from FlareSolverr and apply them to your requests without modifying your existing codebase.
import requests
from flaresolverr_session import Adapter
adapter = Adapter("http://localhost:8191/v1")
session = requests.Session()
session.mount("https://nowsecure.nl", adapter)
session.mount("https://www.nowsecure.nl", adapter) # mount subdomain, mount doesn't support wildcards
response = session.get("https://protected-site.com/page")
print(response.text)
It is recommended only mount the adapter to specific origins that require Cloudflare bypass. Read the caveats section before using it.
Don't use the
Sessionprovided byflaresolverr_sessionhere, they are different concepts.
Caveats
- The FlareSolverr instance and the machine running the adapter must share the same public IP (or use the same proxy with a consistent public IP). Otherwise the cookies obtained from FlareSolverr will not be accepted by Cloudflare.
- The proxy used for the original request is automatically applied to the FlareSolverr request for the reason mentioned above.
- The adapter automatically sends a
GETrequest to the original URL to solve the challenge. You can provide a customchallenge_urlto override this behavior. - Cloudflare cookies are tied to the
User-Agentused during challenge solving. The adapter automatically sets theUser-Agentreturned by FlareSolverr. - The adapter is less reliable than using the Session directly.
How It Works
- The adapter first attempts to send the request normally through its base adapter.
- If it detects a Cloudflare challenge, the adapter forwards the URL to a FlareSolverr instance.
- FlareSolverr solves the challenge and returns cookies and a
User-Agent. - The adapter retries the original request using the returned credentials.
RPC Tool
The flaresolverr_rpc module provides a programmatic interface to the FlareSolverr JSON API, ideal for low-level access to raw API responses. If the Flaresolverr responds a non-OK status, it raise a FlareSolverrResponseError. The RPC class is the underlying tool used by all the features the project provides.
from flaresolverr_session import RPC
with RPC("http://localhost:8191/v1") as rpc:
# Session management
rpc.session.create(session_id="my-session", proxy="http://proxy:8080")
sessions = rpc.session.list()
print(sessions["sessions"])
# HTTP requests
result = rpc.request.get("https://example.com", session_id="my-session")
print(result["solution"]["url"])
print(result["solution"]["response"]) # HTML body
result = rpc.request.post(
"https://example.com",
data="key=value",
session_id="my-session",
)
# Cleanup
rpc.session.destroy("my-session")
All methods return the raw JSON response dict from FlareSolverr.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flaresolverr_session-0.3.1.tar.gz.
File metadata
- Download URL: flaresolverr_session-0.3.1.tar.gz
- Upload date:
- Size: 36.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c055cb16bfa695a0eb4ebfc1677a6a166b1503d5a6c12679d5d03b3aca7d118
|
|
| MD5 |
1212ef2e2d0bc0f2be479a33c49da91f
|
|
| BLAKE2b-256 |
8ec3192dc3fac64c1a424e16d8fa87e793ccd1be85f1f3f4f78a596f6f9dfd55
|
File details
Details for the file flaresolverr_session-0.3.1-py2.py3-none-any.whl.
File metadata
- Download URL: flaresolverr_session-0.3.1-py2.py3-none-any.whl
- Upload date:
- Size: 19.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2cd373c6030f837b621a5a5ddf641de54c7b4f9c718e6243debd75a33608498b
|
|
| MD5 |
bc60b35aed0336c60fae589a6870152b
|
|
| BLAKE2b-256 |
7d42c53abcfcc5b066b9931267b5f4f5d06ebd980e58525af8708e351db1046c
|