flaresolverr-cli 0.2.2__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/PKG-INFO +56 -14
  2. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/README.md +55 -13
  3. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_cli.egg-info/PKG-INFO +56 -14
  4. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_cli.egg-info/SOURCES.txt +6 -2
  5. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_session/__init__.py +5 -1
  6. flaresolverr_cli-0.3.0/flaresolverr_session/adapter.py +168 -0
  7. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_session/cli.py +38 -36
  8. flaresolverr_cli-0.3.0/flaresolverr_session/detection.py +84 -0
  9. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_session/session.py +2 -16
  10. flaresolverr_cli-0.3.0/tests/test_adapter.py +628 -0
  11. flaresolverr_cli-0.3.0/tests/test_challenge.py +139 -0
  12. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/tests/test_cli.py +52 -5
  13. flaresolverr_cli-0.3.0/tests/test_detection.py +93 -0
  14. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/tests/test_rpc.py +128 -95
  15. flaresolverr_cli-0.3.0/tests/test_session.py +530 -0
  16. flaresolverr_cli-0.2.2/tests/test_session.py +0 -556
  17. flaresolverr_cli-0.2.2/tests/testconf.py +0 -47
  18. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/LICENSE +0 -0
  19. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_cli.egg-info/dependency_links.txt +0 -0
  20. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_cli.egg-info/entry_points.txt +0 -0
  21. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_cli.egg-info/requires.txt +0 -0
  22. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_cli.egg-info/top_level.txt +0 -0
  23. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_session/exceptions.py +0 -0
  24. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/flaresolverr_session/rpc.py +0 -0
  25. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/setup.cfg +0 -0
  26. {flaresolverr_cli-0.2.2 → flaresolverr_cli-0.3.0}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flaresolverr-cli
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: A requests.Session that proxies through a FlareSolverr instance.
5
5
  Home-page: https://github.com/Xavier-Lam/FlareSolverrSession
6
6
  Author: Xavier-Lam
@@ -50,7 +50,9 @@ Dynamic: summary
50
50
 
51
51
  A [`requests.Session`](https://docs.python-requests.org/) that transparently routes all HTTP requests through a [FlareSolverr](https://github.com/FlareSolverr/FlareSolverr) instance, allowing you to bypass Cloudflare protection with a familiar Python API.
52
52
 
53
- The project ships with a RPC client for direct access to the FlareSolverr JSON API, and a command-line interface (CLI) for quick requests and session management.
53
+ The package also provides a more powerful [Adapter](#adapter) to handle complex requests if the `Session` is not sufficient.
54
+
55
+ 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.
54
56
 
55
57
  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.
56
58
 
@@ -98,7 +100,7 @@ session = Session(
98
100
  ```
99
101
 
100
102
  #### Response Object
101
- A `FlareSolverr` object is attached to the `response` as `response.flaresolverr`. It contains metadata about the request and challenge solving process returned by *FlareSolverr*.
103
+ 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*.
102
104
 
103
105
  | Attribute | Description |
104
106
  |---|---|
@@ -109,26 +111,36 @@ A `FlareSolverr` object is attached to the `response` as `response.flaresolverr`
109
111
  | `flaresolverr.version` | FlareSolverr server version |
110
112
 
111
113
  #### Exception Handling
112
- All exceptions defined in the module based on `FlareSolverrError`, which inherits from `requests.RequestException`. The inheritance hierarchy is as follows:
114
+ If `FlareSolverr` returns an error response, the session will raise a `FlareSolverrResponseError` exception.
115
+
116
+ All exceptions defined in the module are based on `FlareSolverrError`, which inherits from `requests.RequestException`. The hierarchy is as follows:
113
117
 
114
118
  requests.RequestException
115
119
  └── FlareSolverrError
116
120
  ├── FlareSolverrResponseError
117
- ├── FlareSolverrChallengeError
121
+ └── FlareSolverrChallengeError
118
122
  └── FlareSolverrUnsupportedMethodError
119
123
 
124
+ Exception Details:
120
125
 
121
126
  | Exception | Description |
122
127
  |---|---|
123
128
  | `FlareSolverrResponseError` | FlareSolverr returned an error response. The response dict is available as `response_data` attribute. |
124
- | `FlareSolverrChallengeError` | Challenge solving failed. |
129
+ | `FlareSolverrChallengeError` | Challenge solving failed, raised only in `Session`. |
125
130
  | `FlareSolverrUnsupportedMethodError` | Unsupported HTTP method or content type. |
126
131
 
132
+ #### Limitations
133
+
134
+ - **Only GET and `application/x-www-form-urlencoded` POST** are supported. Otherwise, it will raise `FlareSolverrUnsupportedMethodError`.
135
+ - **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](https://github.com/FlareSolverr/FlareSolverr/issues/1162).
136
+
137
+ > If you need more control over the requests or want to use unsupported methods/content types, consider using the [Adapter](#adapter) instead.
138
+
127
139
  ### Command-Line Interface
128
140
 
129
141
  After installation, you can use the `flaresolverr-cli` command. It is a convenient CLI tool to send HTTP requests through FlareSolverr and manage sessions.
130
142
 
131
- It will output 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`).
143
+ 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`).
132
144
 
133
145
  #### Sending requests
134
146
 
@@ -163,9 +175,45 @@ flaresolverr-cli session destroy my-session
163
175
  flaresolverr-cli session clear
164
176
  ```
165
177
 
178
+ ### Adapter
179
+ 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.
180
+
181
+ ```python
182
+ import requests
183
+ from flaresolverr_session import Adapter
184
+
185
+ adapter = Adapter("http://localhost:8191/v1")
186
+
187
+ session = requests.Session()
188
+ session.mount("https://nowsecure.nl", adapter)
189
+
190
+ response = session.get("https://protected-site.com/page")
191
+ print(response.text)
192
+ ```
193
+
194
+ It is recommended only mount the adapter to specific origins that require Cloudflare bypass. Read the [caveats section](#caveats) before using it.
195
+
196
+ > Don't use the `Session` provided by `flaresolverr_session` here.
197
+
198
+ #### Caveats
199
+
200
+ * 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.
201
+ * The proxy used for the original request is automatically applied to the *FlareSolverr* request for the reason mentioned above.
202
+ * The adapter automatically sends a `GET` request to the original URL to solve the challenge. You can provide a custom `challenge_url` to override this behavior.
203
+ * Cloudflare cookies are tied to the `User-Agent` used during challenge solving. The adapter automatically sets the `User-Agent` returned by FlareSolverr.
204
+ * The adapter is less reliable than using the [Session](#basic-usage) directly.
205
+
206
+ #### How It Works
207
+
208
+ 1. The adapter first attempts to send the request normally through its base adapter.
209
+ 2. If it detects a Cloudflare challenge, the adapter forwards the URL to a FlareSolverr instance.
210
+ 3. FlareSolverr solves the challenge and returns cookies and a `User-Agent`.
211
+ 4. The adapter retries the original request using the returned credentials.
212
+
213
+
166
214
  ### RPC Tool
167
215
 
168
- The `flaresolverr_rpc` module provides a programmatic interface to the FlareSolverr JSON API, useful when you need low-level access to the raw API responses.
216
+ The `flaresolverr_rpc` module provides a programmatic interface to the FlareSolverr JSON API, ideal for low-level access to raw API responses.
169
217
 
170
218
  ```python
171
219
  from flaresolverr_session import RPC
@@ -192,9 +240,3 @@ with RPC("http://localhost:8191/v1") as rpc:
192
240
  ```
193
241
 
194
242
  All methods return the raw JSON response dict from FlareSolverr.
195
-
196
-
197
- ## Limitations
198
-
199
- - **Only GET and `application/x-www-form-urlencoded` POST** are supported. Otherwise, it will raise `FlareSolverrUnsupportedMethodError`.
200
- - **Headers returned by FlareSolverr** may be empty for some sites, depending on the FlareSolverr version and configuration. Empty HTTP status will be regarded as `200`. See [FlareSolverr#1162](https://github.com/FlareSolverr/FlareSolverr/issues/1162).
@@ -6,7 +6,9 @@
6
6
 
7
7
  A [`requests.Session`](https://docs.python-requests.org/) that transparently routes all HTTP requests through a [FlareSolverr](https://github.com/FlareSolverr/FlareSolverr) instance, allowing you to bypass Cloudflare protection with a familiar Python API.
8
8
 
9
- The project ships with a RPC client for direct access to the FlareSolverr JSON API, and a command-line interface (CLI) for quick requests and session management.
9
+ The package also provides a more powerful [Adapter](#adapter) to handle complex requests if the `Session` is not sufficient.
10
+
11
+ 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.
10
12
 
11
13
  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.
12
14
 
@@ -54,7 +56,7 @@ session = Session(
54
56
  ```
55
57
 
56
58
  #### Response Object
57
- A `FlareSolverr` object is attached to the `response` as `response.flaresolverr`. It contains metadata about the request and challenge solving process returned by *FlareSolverr*.
59
+ 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*.
58
60
 
59
61
  | Attribute | Description |
60
62
  |---|---|
@@ -65,26 +67,36 @@ A `FlareSolverr` object is attached to the `response` as `response.flaresolverr`
65
67
  | `flaresolverr.version` | FlareSolverr server version |
66
68
 
67
69
  #### Exception Handling
68
- All exceptions defined in the module based on `FlareSolverrError`, which inherits from `requests.RequestException`. The inheritance hierarchy is as follows:
70
+ If `FlareSolverr` returns an error response, the session will raise a `FlareSolverrResponseError` exception.
71
+
72
+ All exceptions defined in the module are based on `FlareSolverrError`, which inherits from `requests.RequestException`. The hierarchy is as follows:
69
73
 
70
74
  requests.RequestException
71
75
  └── FlareSolverrError
72
76
  ├── FlareSolverrResponseError
73
- ├── FlareSolverrChallengeError
77
+ └── FlareSolverrChallengeError
74
78
  └── FlareSolverrUnsupportedMethodError
75
79
 
80
+ Exception Details:
76
81
 
77
82
  | Exception | Description |
78
83
  |---|---|
79
84
  | `FlareSolverrResponseError` | FlareSolverr returned an error response. The response dict is available as `response_data` attribute. |
80
- | `FlareSolverrChallengeError` | Challenge solving failed. |
85
+ | `FlareSolverrChallengeError` | Challenge solving failed, raised only in `Session`. |
81
86
  | `FlareSolverrUnsupportedMethodError` | Unsupported HTTP method or content type. |
82
87
 
88
+ #### Limitations
89
+
90
+ - **Only GET and `application/x-www-form-urlencoded` POST** are supported. Otherwise, it will raise `FlareSolverrUnsupportedMethodError`.
91
+ - **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](https://github.com/FlareSolverr/FlareSolverr/issues/1162).
92
+
93
+ > If you need more control over the requests or want to use unsupported methods/content types, consider using the [Adapter](#adapter) instead.
94
+
83
95
  ### Command-Line Interface
84
96
 
85
97
  After installation, you can use the `flaresolverr-cli` command. It is a convenient CLI tool to send HTTP requests through FlareSolverr and manage sessions.
86
98
 
87
- It will output 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`).
99
+ 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`).
88
100
 
89
101
  #### Sending requests
90
102
 
@@ -119,9 +131,45 @@ flaresolverr-cli session destroy my-session
119
131
  flaresolverr-cli session clear
120
132
  ```
121
133
 
134
+ ### Adapter
135
+ 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.
136
+
137
+ ```python
138
+ import requests
139
+ from flaresolverr_session import Adapter
140
+
141
+ adapter = Adapter("http://localhost:8191/v1")
142
+
143
+ session = requests.Session()
144
+ session.mount("https://nowsecure.nl", adapter)
145
+
146
+ response = session.get("https://protected-site.com/page")
147
+ print(response.text)
148
+ ```
149
+
150
+ It is recommended only mount the adapter to specific origins that require Cloudflare bypass. Read the [caveats section](#caveats) before using it.
151
+
152
+ > Don't use the `Session` provided by `flaresolverr_session` here.
153
+
154
+ #### Caveats
155
+
156
+ * 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.
157
+ * The proxy used for the original request is automatically applied to the *FlareSolverr* request for the reason mentioned above.
158
+ * The adapter automatically sends a `GET` request to the original URL to solve the challenge. You can provide a custom `challenge_url` to override this behavior.
159
+ * Cloudflare cookies are tied to the `User-Agent` used during challenge solving. The adapter automatically sets the `User-Agent` returned by FlareSolverr.
160
+ * The adapter is less reliable than using the [Session](#basic-usage) directly.
161
+
162
+ #### How It Works
163
+
164
+ 1. The adapter first attempts to send the request normally through its base adapter.
165
+ 2. If it detects a Cloudflare challenge, the adapter forwards the URL to a FlareSolverr instance.
166
+ 3. FlareSolverr solves the challenge and returns cookies and a `User-Agent`.
167
+ 4. The adapter retries the original request using the returned credentials.
168
+
169
+
122
170
  ### RPC Tool
123
171
 
124
- The `flaresolverr_rpc` module provides a programmatic interface to the FlareSolverr JSON API, useful when you need low-level access to the raw API responses.
172
+ The `flaresolverr_rpc` module provides a programmatic interface to the FlareSolverr JSON API, ideal for low-level access to raw API responses.
125
173
 
126
174
  ```python
127
175
  from flaresolverr_session import RPC
@@ -148,9 +196,3 @@ with RPC("http://localhost:8191/v1") as rpc:
148
196
  ```
149
197
 
150
198
  All methods return the raw JSON response dict from FlareSolverr.
151
-
152
-
153
- ## Limitations
154
-
155
- - **Only GET and `application/x-www-form-urlencoded` POST** are supported. Otherwise, it will raise `FlareSolverrUnsupportedMethodError`.
156
- - **Headers returned by FlareSolverr** may be empty for some sites, depending on the FlareSolverr version and configuration. Empty HTTP status will be regarded as `200`. See [FlareSolverr#1162](https://github.com/FlareSolverr/FlareSolverr/issues/1162).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flaresolverr-cli
3
- Version: 0.2.2
3
+ Version: 0.3.0
4
4
  Summary: A requests.Session that proxies through a FlareSolverr instance.
5
5
  Home-page: https://github.com/Xavier-Lam/FlareSolverrSession
6
6
  Author: Xavier-Lam
@@ -50,7 +50,9 @@ Dynamic: summary
50
50
 
51
51
  A [`requests.Session`](https://docs.python-requests.org/) that transparently routes all HTTP requests through a [FlareSolverr](https://github.com/FlareSolverr/FlareSolverr) instance, allowing you to bypass Cloudflare protection with a familiar Python API.
52
52
 
53
- The project ships with a RPC client for direct access to the FlareSolverr JSON API, and a command-line interface (CLI) for quick requests and session management.
53
+ The package also provides a more powerful [Adapter](#adapter) to handle complex requests if the `Session` is not sufficient.
54
+
55
+ 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.
54
56
 
55
57
  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.
56
58
 
@@ -98,7 +100,7 @@ session = Session(
98
100
  ```
99
101
 
100
102
  #### Response Object
101
- A `FlareSolverr` object is attached to the `response` as `response.flaresolverr`. It contains metadata about the request and challenge solving process returned by *FlareSolverr*.
103
+ 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*.
102
104
 
103
105
  | Attribute | Description |
104
106
  |---|---|
@@ -109,26 +111,36 @@ A `FlareSolverr` object is attached to the `response` as `response.flaresolverr`
109
111
  | `flaresolverr.version` | FlareSolverr server version |
110
112
 
111
113
  #### Exception Handling
112
- All exceptions defined in the module based on `FlareSolverrError`, which inherits from `requests.RequestException`. The inheritance hierarchy is as follows:
114
+ If `FlareSolverr` returns an error response, the session will raise a `FlareSolverrResponseError` exception.
115
+
116
+ All exceptions defined in the module are based on `FlareSolverrError`, which inherits from `requests.RequestException`. The hierarchy is as follows:
113
117
 
114
118
  requests.RequestException
115
119
  └── FlareSolverrError
116
120
  ├── FlareSolverrResponseError
117
- ├── FlareSolverrChallengeError
121
+ └── FlareSolverrChallengeError
118
122
  └── FlareSolverrUnsupportedMethodError
119
123
 
124
+ Exception Details:
120
125
 
121
126
  | Exception | Description |
122
127
  |---|---|
123
128
  | `FlareSolverrResponseError` | FlareSolverr returned an error response. The response dict is available as `response_data` attribute. |
124
- | `FlareSolverrChallengeError` | Challenge solving failed. |
129
+ | `FlareSolverrChallengeError` | Challenge solving failed, raised only in `Session`. |
125
130
  | `FlareSolverrUnsupportedMethodError` | Unsupported HTTP method or content type. |
126
131
 
132
+ #### Limitations
133
+
134
+ - **Only GET and `application/x-www-form-urlencoded` POST** are supported. Otherwise, it will raise `FlareSolverrUnsupportedMethodError`.
135
+ - **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](https://github.com/FlareSolverr/FlareSolverr/issues/1162).
136
+
137
+ > If you need more control over the requests or want to use unsupported methods/content types, consider using the [Adapter](#adapter) instead.
138
+
127
139
  ### Command-Line Interface
128
140
 
129
141
  After installation, you can use the `flaresolverr-cli` command. It is a convenient CLI tool to send HTTP requests through FlareSolverr and manage sessions.
130
142
 
131
- It will output 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`).
143
+ 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`).
132
144
 
133
145
  #### Sending requests
134
146
 
@@ -163,9 +175,45 @@ flaresolverr-cli session destroy my-session
163
175
  flaresolverr-cli session clear
164
176
  ```
165
177
 
178
+ ### Adapter
179
+ 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.
180
+
181
+ ```python
182
+ import requests
183
+ from flaresolverr_session import Adapter
184
+
185
+ adapter = Adapter("http://localhost:8191/v1")
186
+
187
+ session = requests.Session()
188
+ session.mount("https://nowsecure.nl", adapter)
189
+
190
+ response = session.get("https://protected-site.com/page")
191
+ print(response.text)
192
+ ```
193
+
194
+ It is recommended only mount the adapter to specific origins that require Cloudflare bypass. Read the [caveats section](#caveats) before using it.
195
+
196
+ > Don't use the `Session` provided by `flaresolverr_session` here.
197
+
198
+ #### Caveats
199
+
200
+ * 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.
201
+ * The proxy used for the original request is automatically applied to the *FlareSolverr* request for the reason mentioned above.
202
+ * The adapter automatically sends a `GET` request to the original URL to solve the challenge. You can provide a custom `challenge_url` to override this behavior.
203
+ * Cloudflare cookies are tied to the `User-Agent` used during challenge solving. The adapter automatically sets the `User-Agent` returned by FlareSolverr.
204
+ * The adapter is less reliable than using the [Session](#basic-usage) directly.
205
+
206
+ #### How It Works
207
+
208
+ 1. The adapter first attempts to send the request normally through its base adapter.
209
+ 2. If it detects a Cloudflare challenge, the adapter forwards the URL to a FlareSolverr instance.
210
+ 3. FlareSolverr solves the challenge and returns cookies and a `User-Agent`.
211
+ 4. The adapter retries the original request using the returned credentials.
212
+
213
+
166
214
  ### RPC Tool
167
215
 
168
- The `flaresolverr_rpc` module provides a programmatic interface to the FlareSolverr JSON API, useful when you need low-level access to the raw API responses.
216
+ The `flaresolverr_rpc` module provides a programmatic interface to the FlareSolverr JSON API, ideal for low-level access to raw API responses.
169
217
 
170
218
  ```python
171
219
  from flaresolverr_session import RPC
@@ -192,9 +240,3 @@ with RPC("http://localhost:8191/v1") as rpc:
192
240
  ```
193
241
 
194
242
  All methods return the raw JSON response dict from FlareSolverr.
195
-
196
-
197
- ## Limitations
198
-
199
- - **Only GET and `application/x-www-form-urlencoded` POST** are supported. Otherwise, it will raise `FlareSolverrUnsupportedMethodError`.
200
- - **Headers returned by FlareSolverr** may be empty for some sites, depending on the FlareSolverr version and configuration. Empty HTTP status will be regarded as `200`. See [FlareSolverr#1162](https://github.com/FlareSolverr/FlareSolverr/issues/1162).
@@ -8,11 +8,15 @@ flaresolverr_cli.egg-info/entry_points.txt
8
8
  flaresolverr_cli.egg-info/requires.txt
9
9
  flaresolverr_cli.egg-info/top_level.txt
10
10
  flaresolverr_session/__init__.py
11
+ flaresolverr_session/adapter.py
11
12
  flaresolverr_session/cli.py
13
+ flaresolverr_session/detection.py
12
14
  flaresolverr_session/exceptions.py
13
15
  flaresolverr_session/rpc.py
14
16
  flaresolverr_session/session.py
17
+ tests/test_adapter.py
18
+ tests/test_challenge.py
15
19
  tests/test_cli.py
20
+ tests/test_detection.py
16
21
  tests/test_rpc.py
17
- tests/test_session.py
18
- tests/testconf.py
22
+ tests/test_session.py
@@ -1,5 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
+ from flaresolverr_session.adapter import Adapter
4
+ from flaresolverr_session.detection import is_cloudflare_challenge
3
5
  from flaresolverr_session.exceptions import (
4
6
  FlareSolverrError,
5
7
  FlareSolverrResponseError,
@@ -12,11 +14,13 @@ from flaresolverr_session.session import Session, Response
12
14
  __title__ = "flaresolverr-session"
13
15
  __description__ = "A requests.Session that proxies through a FlareSolverr instance."
14
16
  __url__ = "https://github.com/Xavier-Lam/FlareSolverrSession"
15
- __version__ = "0.2.2"
17
+ __version__ = "0.3.0"
16
18
  __author__ = "Xavier-Lam"
17
19
  __author_email__ = "xavierlam7@hotmail.com"
18
20
 
19
21
  __all__ = [
22
+ "Adapter",
23
+ "is_cloudflare_challenge",
20
24
  "Session",
21
25
  "Response",
22
26
  "RPC",
@@ -0,0 +1,168 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import warnings
4
+
5
+ try:
6
+ from urlparse import urlparse, urlunparse
7
+ except ImportError:
8
+ from urllib.parse import urlparse, urlunparse
9
+
10
+ from requests.adapters import BaseAdapter, HTTPAdapter
11
+ from requests.cookies import RequestsCookieJar
12
+ from requests.utils import select_proxy
13
+
14
+ from flaresolverr_session.detection import is_cloudflare_challenge
15
+ from flaresolverr_session.rpc import RPC
16
+
17
+
18
+ class Adapter(BaseAdapter):
19
+ """A ``requests`` transport adapter that retries requests blocked by
20
+ Cloudflare by solving challenges through FlareSolverr.
21
+
22
+ Normal (non-challenged) requests are forwarded directly through the
23
+ *base_adapter*. When a Cloudflare challenge is detected the adapter
24
+ automatically:
25
+
26
+ 1. Sends the *challenge_url* (or the blocked URL) to FlareSolverr.
27
+ 2. Caches the ``cf_clearance`` cookie and User-Agent per site.
28
+ 3. Retries the original request with the obtained cookie and UA.
29
+
30
+ Parameters:
31
+ flaresolverr_url (str or None): The FlareSolverr API endpoint.
32
+ Ignored when *rpc* is provided.
33
+ rpc (flaresolverr_session.rpc.RPC or None):
34
+ A pre-configured :class:`~flaresolverr_session.rpc.RPC`
35
+ instance. When provided, *flaresolverr_url* is ignored.
36
+ challenge_url (str or None): URL to use for solving challenges.
37
+ When *None*, the adapter sends the blocked URL itself for
38
+ challenge solving. Can be an absolute path without domain
39
+ (e.g. ``"/"``) — it will be combined with the domain of
40
+ the blocked URL.
41
+ base_adapter (requests.adapters.BaseAdapter or None): The
42
+ adapter used to perform actual HTTP requests.
43
+ """
44
+
45
+ def __init__(
46
+ self,
47
+ flaresolverr_url=None,
48
+ rpc=None,
49
+ challenge_url=None,
50
+ base_adapter=None,
51
+ ):
52
+ super(Adapter, self).__init__()
53
+
54
+ if rpc is not None and flaresolverr_url is not None:
55
+ warnings.warn(
56
+ "Both 'rpc' and 'flaresolverr_url' are provided. "
57
+ "The 'rpc' instance will be used and "
58
+ "'flaresolverr_url' will be ignored.",
59
+ stacklevel=2,
60
+ )
61
+ if not rpc:
62
+ rpc = RPC(flaresolverr_url)
63
+ self._rpc = rpc
64
+ self._challenge_url = challenge_url
65
+
66
+ if base_adapter is None:
67
+ base_adapter = HTTPAdapter()
68
+ self._base_adapter = base_adapter
69
+
70
+ # Per-site caches keyed by hostname.
71
+ self._cf_cookies = {}
72
+ self._user_agents = {}
73
+
74
+ def send(
75
+ self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None
76
+ ):
77
+ kwargs = dict(
78
+ stream=stream,
79
+ timeout=timeout,
80
+ verify=verify,
81
+ cert=cert,
82
+ proxies=proxies,
83
+ )
84
+
85
+ self._prepare_request(request)
86
+ response = self._base_adapter.send(request, **kwargs)
87
+
88
+ if is_cloudflare_challenge(response):
89
+ self._solve_challenge(request.url, proxies=proxies)
90
+ self._prepare_request(request)
91
+ response = self._base_adapter.send(request, **kwargs)
92
+
93
+ return response
94
+
95
+ def _prepare_request(self, request):
96
+ parsed = urlparse(request.url)
97
+ domain = parsed.hostname
98
+ path = parsed.path or "/"
99
+
100
+ jar = self._cf_cookies.get(domain)
101
+ if jar:
102
+ # Merge cookies into the Cookie header.
103
+ existing = RequestsCookieJar()
104
+ # Parse existing Cookie header if present.
105
+ cookie_header = request.headers.get("Cookie")
106
+ if cookie_header:
107
+ for pair in cookie_header.split(";"):
108
+ pair = pair.strip()
109
+ if "=" in pair:
110
+ name, value = pair.split("=", 1)
111
+ existing.set(name.strip(), value.strip())
112
+ # Overlay cached CF cookies that match the request path.
113
+ for cookie in jar:
114
+ if path.startswith(cookie.path):
115
+ existing.set(cookie.name, cookie.value)
116
+
117
+ # Re-build Cookie header.
118
+ cookie_str = "; ".join("%s=%s" % (c.name, c.value) for c in existing)
119
+ if cookie_str:
120
+ request.headers["Cookie"] = cookie_str
121
+
122
+ user_agent = self._user_agents.get(domain)
123
+ if user_agent:
124
+ request.headers["User-Agent"] = user_agent
125
+
126
+ def _solve_challenge(self, original_url, proxies=None):
127
+ challenge_url = self._get_challenge_url(original_url)
128
+ rpc_kwargs = {"url": challenge_url}
129
+
130
+ if proxies:
131
+ proxy_url = select_proxy(challenge_url, proxies)
132
+ if proxy_url:
133
+ rpc_kwargs["proxy"] = {"url": proxy_url}
134
+
135
+ data = self._rpc.request.get(**rpc_kwargs)
136
+ solution = data.get("solution", {})
137
+
138
+ domain = urlparse(original_url).hostname
139
+
140
+ user_agent = solution.get("userAgent")
141
+ if user_agent:
142
+ self._user_agents[domain] = user_agent
143
+
144
+ jar = RequestsCookieJar()
145
+ for cd in solution.get("cookies", []):
146
+ if cd.get("name") == "cf_clearance":
147
+ jar.set(
148
+ cd["name"],
149
+ cd.get("value", ""),
150
+ domain=cd.get("domain"),
151
+ path=cd.get("path", "/"),
152
+ secure=cd.get("secure", False),
153
+ expires=cd.get("expiry"),
154
+ )
155
+ break
156
+ self._cf_cookies[domain] = jar
157
+
158
+ def _get_challenge_url(self, original_url):
159
+ if self._challenge_url is None:
160
+ return original_url
161
+
162
+ if self._challenge_url.startswith("/"):
163
+ parsed = urlparse(original_url)
164
+ return urlunparse(
165
+ (parsed.scheme, parsed.netloc, self._challenge_url, "", "", "")
166
+ )
167
+
168
+ return self._challenge_url