### Install Dependencies and Browsers Source: https://github.com/art3m4ik3/cloudflare-solver/blob/main/README.md Commands to set up the project environment by installing Python dependencies and the required Playwright browser binaries. ```bash pip install -r requirements.txt playwright install ``` -------------------------------- ### Solve Cloudflare Challenges with Python Source: https://github.com/art3m4ik3/cloudflare-solver/blob/main/README.md Examples demonstrating how to initialize the CloudflareSolver for both cookie-based challenges and Turnstile token challenges. These scripts show the basic implementation pattern for automated verification. ```python from main import CloudflareSolver, ChallengeType import asyncio async def main(): solver = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, headless=True, os=["windows"], ) result = await solver.solve("https://nopecha.com/demo/cloudflare") if result: print(f"Cookie obtained: {result.name}={result.value}") else: print("Failed to solve Cloudflare challenge") asyncio.run(main()) ``` ```python from main import CloudflareSolver, ChallengeType import asyncio async def main(): solver = CloudflareSolver( challenge_type=ChallengeType.TURNSTILE, headless=True, os=["windows"], ) result = await solver.solve("https://nopecha.com/captcha/turnstile") if result: print(f"Token obtained: {result.token}") else: print("Failed to solve Turnstile challenge") asyncio.run(main()) ``` -------------------------------- ### Manage TurnstileToken Dataclass Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt Demonstrates the use of the TurnstileToken dataclass for storing and validating Cloudflare Turnstile tokens. Includes an example of integrating the token into a standard HTTP request. ```python from main import TurnstileToken import requests # Create token instance token = TurnstileToken(token="0.abcdefghijklmnop...") # Access token value print(f"Token: {token.token}") # Validation on empty token try: invalid_token = TurnstileToken(token="") except ValueError as e: print(f"Error: {e}") # Use with form submission response = requests.post( "https://example.com/verify", data={"cf-turnstile-response": token.token} ) ``` -------------------------------- ### Configure Proxy and Advanced Settings Source: https://github.com/art3m4ik3/cloudflare-solver/blob/main/README.md Demonstrates how to integrate proxy servers and customize solver behavior such as headless mode, OS fingerprinting, and retry logic. ```python from main import CloudflareSolver, ChallengeType import asyncio async def main(): proxy_url = "http://user:password@123.45.67.89:8080" solver = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, proxy=proxy_url, headless=True ) result = await solver.solve("https://nopecha.com/demo/cloudflare") if result: print(f"Success! Cookie: {result.value[:20]}...") asyncio.run(main()) ``` ```python solver = CloudflareSolver( challenge_type=ChallengeType.TURNSTILE, sleep_time=5, headless=False, os=["macos"], debug=True, retries=50, proxy="http://user:pass@host:port" ) ``` -------------------------------- ### Initialize CloudflareSolver Instance Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt The CloudflareSolver class manages browser automation. It accepts configurations for challenge types, headless mode, OS fingerprinting, and network proxy settings. ```python from main import CloudflareSolver, ChallengeType # Basic initialization with defaults solver = CloudflareSolver() # Full configuration with all options solver = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, # Type of challenge to solve sleep_time=5, # Seconds to wait before clicking headless=True, # Run browser without GUI os=["windows"], # OS fingerprint: "windows", "macos", "linux" debug=False, # Enable debug logging retries=30, # Max attempts to find challenge frame proxy=None # Proxy URL: "http://user:pass@host:port" ) ``` -------------------------------- ### Configure Proxy for CloudflareSolver Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt Demonstrates how to route traffic through HTTP proxies with or without authentication. This is essential for IP rotation and bypassing geographic restrictions during challenge solving. ```python import asyncio from main import CloudflareSolver, ChallengeType async def solve_with_proxy(): # HTTP proxy with authentication solver = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, proxy="http://username:password@proxy.example.com:8080", headless=True ) # HTTP proxy without authentication solver_simple = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, proxy="http://192.168.1.100:3128", headless=True ) result = await solver.solve("https://protected-site.com") if result: print(f"Success via proxy: {result.value[:50]}...") else: print("Challenge failed - try different proxy") asyncio.run(solve_with_proxy()) ``` -------------------------------- ### Enable Debug Mode for Troubleshooting Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt Shows how to enable verbose logging and automatic screenshot capture upon failure. Useful for diagnosing issues with challenge detection and browser automation behavior. ```python import asyncio import logging from main import CloudflareSolver, ChallengeType async def debug_solving(): # Debug mode enables logging and saves screenshots on failure solver = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, debug=True, # Enables DEBUG level logging headless=False, # Show browser for visual debugging sleep_time=8, # Longer delay to observe behavior retries=50 # More attempts for difficult challenges ) result = await solver.solve("https://example.com/protected") if result: print("Challenge solved successfully") else: print("Check debug_failed_challenge.png for screenshot") asyncio.run(debug_solving()) ``` -------------------------------- ### Python Cloudflare Challenge and Turnstile Solver with Requests Integration Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt This Python script demonstrates how to use the CloudflareSolver to bypass Cloudflare's protection for both standard challenges and Turnstile. It includes functions to solve the challenges, process the results into usable cookies or tokens, and then integrate these with the 'requests' library for making subsequent authenticated HTTP requests to protected resources or submitting login forms. ```python import asyncio from typing import Optional, Dict, Any from main import ( CloudflareSolver, ChallengeType, CloudflareCookie, TurnstileToken ) async def get_cloudflare_credentials( url: str, challenge_type: ChallengeType = ChallengeType.CHALLENGE, proxy: Optional[str] = None ) -> Optional[Dict[str, Any]]: """Solve Cloudflare challenge and return credentials for requests.""" solver = CloudflareSolver( challenge_type=challenge_type, headless=True, os=["windows", "macos"], # Random OS selection sleep_time=5, retries=30, proxy=proxy, debug=False ) result = await solver.solve(url) if result is None: return None if isinstance(result, CloudflareCookie): return { "type": "cookie", "cookies": {result.name: result.value}, "domain": result.domain, "expires": result.expires } elif isinstance(result, TurnstileToken): return { "type": "token", "token": result.token } return None async def main(): # Solve Challenge type challenge_result = await get_cloudflare_credentials( url="https://protected-site.com", challenge_type=ChallengeType.CHALLENGE ) if challenge_result and challenge_result["type"] == "cookie": import requests session = requests.Session() session.cookies.update(challenge_result["cookies"]) response = session.get("https://protected-site.com/api/data") print(f"API Response: {response.status_code}") # Solve Turnstile type turnstile_result = await get_cloudflare_credentials( url="https://site-with-turnstile.com/login", challenge_type=ChallengeType.TURNSTILE ) if turnstile_result and turnstile_result["type"] == "token": import requests response = requests.post( "https://site-with-turnstile.com/login", data={ "cf-turnstile-response": turnstile_result["token"], "email": "user@example.com", "password": "secret" } ) print(f"Login Response: {response.status_code}") asyncio.run(main()) ``` -------------------------------- ### Manage CloudflareCookie Dataclass Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt Explains how to instantiate and validate Cloudflare clearance cookies. Supports creation from dictionary objects and direct validation of cookie attributes. ```python from main import CloudflareCookie # Create from dictionary (as returned by Playwright) cookie_dict = { "name": "cf_clearance", "value": "abc123xyz...", "domain": ".example.com", "path": "/", "expires": 1735689600, "httpOnly": True, "secure": True, "sameSite": "Lax" } cookie = CloudflareCookie.from_json(cookie_dict) # Access cookie properties print(f"Name: {cookie.name}") print(f"Value: {cookie.value}") print(f"Domain: {cookie.domain}") print(f"Expires: {cookie.expires}") # Direct instantiation with validation try: cookie = CloudflareCookie( name="cf_clearance", value="token_value", domain=".example.com", path="/", expires=1735689600, http_only=True, secure=True, same_site="Lax" ) except ValueError as e: print(f"Invalid cookie: {e}") ``` -------------------------------- ### Solve Traditional Cloudflare Challenge Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt Use the solve method to obtain a cf_clearance cookie. The returned dataclass contains cookie metadata necessary for authenticated requests. ```python import asyncio from main import CloudflareSolver, ChallengeType, CloudflareCookie async def solve_challenge(): solver = CloudflareSolver( challenge_type=ChallengeType.CHALLENGE, headless=True, os=["windows"], retries=30 ) result = await solver.solve("https://example.com/protected-page") if result and isinstance(result, CloudflareCookie): print(f"Cookie Name: {result.name}") print(f"Cookie Value: {result.value}") # Use cookie with requests library import requests cookies = {result.name: result.value} response = requests.get("https://example.com/api", cookies=cookies) else: print("Failed to solve challenge") asyncio.run(solve_challenge()) ``` -------------------------------- ### Solve Cloudflare Turnstile Challenge Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt The solve method extracts a verification token for Turnstile captchas. This token is typically submitted as form data to bypass the challenge. ```python import asyncio from main import CloudflareSolver, ChallengeType, TurnstileToken async def solve_turnstile(): solver = CloudflareSolver( challenge_type=ChallengeType.TURNSTILE, headless=True, os=["macos"], sleep_time=5 ) result = await solver.solve("https://example.com/turnstile-page") if result and isinstance(result, TurnstileToken): print(f"Token: {result.token}") # Use token in form submission import requests form_data = { "cf-turnstile-response": result.token, "username": "user", "password": "pass" } response = requests.post("https://example.com/login", data=form_data) else: print("Failed to solve Turnstile challenge") asyncio.run(solve_turnstile()) ``` -------------------------------- ### Define Challenge Types with ChallengeType Enum Source: https://context7.com/art3m4ik3/cloudflare-solver/llms.txt The ChallengeType enum specifies the target protection mechanism. Use CHALLENGE for cookie-based protection or TURNSTILE for token-based captcha systems. ```python from main import ChallengeType # Available challenge types challenge_mode = ChallengeType.CHALLENGE # Returns cf_clearance cookie turnstile_mode = ChallengeType.TURNSTILE # Returns verification token # Check enum values print(ChallengeType.CHALLENGE.value) # Output: "challenge" print(ChallengeType.TURNSTILE.value) # Output: "turnstile" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.