### Use Private Proxies with Requests Source: https://context7.com/kuaidaili/python-sdk/llms.txt Demonstrates how to integrate private proxies with the 'requests' library for making HTTP/HTTPS requests. Includes examples for both username/password and whitelist authentication. ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import random api_url = "http://dps.kdlapi.com/api/getdps?secret_id=o1fjh1re9o28876h7c08&signature=xxxxx&num=10&format=json&sep=1" proxy_response = requests.get(api_url).json() proxy_list = proxy_response['data']['proxy_list'] username = "your_username" password = "your_password" proxy = random.choice(proxy_list) proxies = { "http": f"http://{username}:{password}@{proxy}/", "https": f"http://{username}:{password}@{proxy}/" } # For whitelist authentication (no username/password needed) # proxies = { # "http": f"http://{proxy}/", # "https": f"http://{proxy}/" # } target_url = "http://dev.kdlapi.com/testproxy" headers = { "Accept-Encoding": "Gzip", # Enable gzip compression } response = requests.get(target_url, proxies=proxies, headers=headers) print("Status code:", response.status_code) if response.status_code == 200: response.encoding = "utf-8" print("Response content:", response.content.decode('utf-8')) ``` -------------------------------- ### Get Proxy Authentication Credentials Source: https://context7.com/kuaidaili/python-sdk/llms.txt Retrieves proxy authentication credentials from the client. Supports plain text output and token-based signing. ```python proxyauth = client.get_proxy_authorization(plain_text=1, sign_type='token') print("Auth info:", proxyauth) ``` -------------------------------- ### Get Open Proxy IPs (OPS) with Order Level Source: https://context7.com/kuaidaili/python-sdk/llms.txt Fetches open proxy IPs (OPS) with specified order levels, allowing selection of different service tiers like NORMAL, SVIP, and PRO. Includes functionality to check the validity of fetched proxies. ```python import kdl from kdl.utils import OpsOrderLevel auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) ops_ips = client.get_proxy( num=4, sign_type='token', order_level=OpsOrderLevel.SVIP, format='json', pt=2, area='北京,上海,广东' ) print("Open proxies:", ops_ips) valids = client.check_ops_valid(ops_ips) print("Validity:", valids) ``` -------------------------------- ### Utility Functions Source: https://context7.com/kuaidaili/python-sdk/llms.txt Access helper functions for User-Agent generation and area code lookups. ```APIDOC ## Utility Functions ### Description This section covers utility functions provided by the Kuaidaili Python SDK, including generating random User-Agent strings and looking up area codes. ### Methods - `get_ua(num, format)`: Retrieves a specified number of random User-Agent strings. - `get_area_code(area, format)`: Gets the area code for a given location. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None (Parameters are passed as arguments to the methods) ##### `get_ua` specific parameters: - **num** (integer) - Optional - The number of User-Agents to retrieve. Defaults to 1. - **format** (string) - Optional - The desired output format (e.g., 'json'). Defaults to raw string. ##### `get_area_code` specific parameters: - **area** (string) - Required - The name of the area (e.g., '北京'). - **format** (string) - Optional - The desired output format (e.g., 'json'). Defaults to raw string. ### Request Example ```python import kdl auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Get random User-Agent strings user_agents = client.get_ua(num=5, format='json') print("User agents:", user_agents) # Get area code for specific location area_code = client.get_area_code(area='北京', format='json') print("Area code:", area_code) ``` ### Response #### Success Response (200) Responses include the requested data in the specified format. For `get_ua`, it's a list of User-Agent strings. For `get_area_code`, it's an object containing the area code and name. #### Response Example ```json [ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36..." ] ``` ```json { "code": 0, "data": { "area_code": "110000", "area_name": "北京" } } ``` ``` -------------------------------- ### Use Tunnel Proxies with Requests Source: https://context7.com/kuaidaili/python-sdk/llms.txt Shows how to connect through a persistent tunnel proxy endpoint using the 'requests' library. Supports both username/password and whitelist authentication methods. ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import requests tunnel = "tps123456.kdlapi.com:15818" username = "your_tunnel_username" password = "your_tunnel_password" proxies = { "http": f"http://{username}:{password}@{tunnel}/", "https": f"http://{username}:{password}@{tunnel}/" } # Whitelist authentication (requires IP whitelist setup) # proxies = { # "http": f"http://{tunnel}/", # "https": f"http://{tunnel}/" # } target_url = "https://dev.kdlapi.com/testproxy" response = requests.get(target_url, proxies=proxies) if response.status_code == 200: print("Response:", response.text) else: print(f"Error: {response.status_code}") ``` -------------------------------- ### Get Dedicated Proxy IPs (KPS) with Kuaidaili Python SDK Source: https://context7.com/kuaidaili/python-sdk/llms.txt Retrieves dedicated proxy IPs (KPS) with options for geographic targeting. Supports filtering by protocol type and area, similar to DPS. Requires authentication credentials. ```python import kdl auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Extract 2 dedicated proxies from specific regions kps_ips = client.get_kps( num=2, sign_type='hmacsha1', format='json', pt=2, area='北京,上海,广东' ) print("Dedicated proxies:", kps_ips) # Output: ['123.45.67.89:8888', '98.76.54.32:8888'] ``` -------------------------------- ### Proxy Downloader Middleware Source: https://context7.com/kuaidaili/python-sdk/llms.txt Custom Scrapy middleware for proxy rotation. Add to DOWNLOADER_MIDDLEWARES in settings.py. ```APIDOC ## Proxy Downloader Middleware ### Description Custom Scrapy middleware for proxy rotation. This middleware randomly selects a proxy from a predefined list and configures it for use with requests, including optional username and password authentication. ### Method `process_request(self, request, spider)` ### Endpoint N/A (This is a middleware class) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example (Refer to the Python SDK code for configuration) ### Response #### Success Response (200) Returns `None` to indicate the request should proceed. #### Response Example None ``` -------------------------------- ### Initialize Kuaidaili Python SDK Client Source: https://context7.com/kuaidaili/python-sdk/llms.txt Initializes the Kuaidaili client with authentication credentials. Supports custom timeout and retry settings. Requires secret_id and secret_key for authentication. ```python # -*- coding: utf-8 -*\ import kdl # Create authentication object with your credentials auth = kdl.Auth("your_secret_id", "your_secret_key") # Initialize client with custom timeout and retry settings # timeout: (connect_timeout, read_timeout) in seconds # max_retries: number of retry attempts for failed requests client = kdl.Client(auth, timeout=(8, 12), max_retries=3) # Simple initialization with defaults client = kdl.Client(auth) # Default timeout: (6, 8) seconds # Default max_retries: None ``` -------------------------------- ### Get Private Proxy IPs (DPS) with Kuaidaili Python SDK Source: https://context7.com/kuaidaili/python-sdk/llms.txt Retrieves private proxy IPs (DPS) with options for filtering by region and protocol type. Supports different sign types and format options. Includes functions to check proxy validity and remaining time, as well as get IP balance for metered orders. ```python import kdl auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Extract 5 private proxy IPs with specific parameters # sign_type: 'token' (default), 'hmacsha1', or 'simple' # format: 'json' returns structured data # pt: protocol type (1=HTTP, 2=HTTPS, 3=SOCKS5) # area: comma-separated city names (use unicode for Python 2) ips = client.get_dps( num=5, sign_type='hmacsha1', format='json', pt=2, area='北京,上海,广东' ) print("Private proxies:", ips) # Output: ['113.120.61.166:22989', '122.4.44.132:21808', ...] # Check proxy validity valids = client.check_dps_valid(ips) print("Validity status:", valids) # Output: {'113.120.61.166:22989': True, '122.4.44.132:21808': False, ...} # Get remaining time for each proxy in seconds seconds = client.get_dps_valid_time(ips) print("Remaining time:", seconds) # Output: {'113.120.61.166:22989': 3600, '122.4.44.132:21808': 7200, ...} # Get IP balance for metered orders balance = client.get_ip_balance(sign_type='hmacsha1') print("IP balance:", balance) # Output: 1500 ``` -------------------------------- ### Manage Tunnel Proxy Configuration (TPS) with Kuaidaili Python SDK Source: https://context7.com/kuaidaili/python-sdk/llms.txt Manages tunnel proxy (TPS) settings, including order expiration time, IP whitelist configuration, and dynamic IP switching. Provides functions to get the current tunnel IP and manually change it. Also retrieves proxy authentication credentials. ```python import kdl auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Get order expiration time expire_time = client.get_order_expire_time() print("Order expires:", expire_time) # Output: "2025-12-31 23:59:59" # Manage IP whitelist (required for whitelist authentication) # Set whitelist with list, tuple, or comma-separated string client.set_ip_whitelist(["171.113.244.40", "171.113.244.41"]) # Or as string client.set_ip_whitelist("171.113.244.40,171.113.244.41") # Get current whitelist whitelist = client.get_ip_whitelist() print("Current whitelist:", whitelist) # Output: ['171.113.244.40', '171.113.244.41'] # Get current tunnel IP (for tunnels with >=1 minute IP rotation) current_ip = client.tps_current_ip() print("Current tunnel IP:", current_ip) # Output: "203.0.113.45" # Change tunnel IP immediately (manual rotation) new_ip = client.change_tps_ip() print("New tunnel IP:", new_ip) # Output: "203.0.113.89" # Get tunnel proxy authentication credentials auth_info = client.get_proxy_authorization(plain_text=1, sign_type='token') print("Username:", auth_info['username']) print("Password:", auth_info['password']) # Output: Username: t12345678901234, Password: abcd1234efgh ``` -------------------------------- ### Order Management and Account Operations Source: https://context7.com/kuaidaili/python-sdk/llms.txt Manage orders, check balances, and configure auto-renewal for proxy subscriptions using the Kuaidaili client. ```APIDOC ## Order Management and Account Operations ### Description This section details how to manage your proxy orders, check your account balance, and configure auto-renewal settings using the Kuaidaili Python client. ### Methods - `get_account_balance()`: Retrieves the current account balance. - `create_order(product, pay_type, sign_type)`: Creates a new order for a proxy product. - `get_order_info(sign_type)`: Fetches detailed information about existing orders. - `set_auto_renew(autorenew, sign_type)`: Enables or disables auto-renewal for an order. - `close_order(sign_type)`: Closes a post-paid order. - `query_kps_city(serie, sign_type)`: Queries available cities for dedicated proxy services. ### Parameters #### Path Parameters None #### Query Parameters - `sign_type` (string) - Required - The signature type for the request (e.g., 'hmacsha1'). #### Request Body None (Parameters are passed as arguments to the methods) ##### `create_order` specific parameters: - **product** (string) - Required - The product code (e.g., 'dps_basic'). - **pay_type** (integer) - Required - Payment type: 1=daily, 2=monthly, 3=yearly. ##### `set_auto_renew` specific parameters: - **autorenew** (integer) - Required - Renewal status: 1=enable, 0=disable. ##### `query_kps_city` specific parameters: - **serie** (string) - Required - The series of the dedicated proxy (e.g., 'kps_basic'). ### Request Example ```python import kdl auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Get account balance balance = client.get_account_balance() print("Account balance:", balance) # Create new order order_result = client.create_order(product='dps_basic', pay_type=2, sign_type='hmacsha1') print("Order created:", order_result) ``` ### Response #### Success Response (200) Responses vary depending on the method called. Typically includes a `code` and `msg` indicating success or failure, and relevant data (e.g., balance, order details, city lists). #### Response Example ```json { "balance": "1580.50", "currency": "CNY" } ``` ``` -------------------------------- ### Kuaidaili Account and Order Operations (Python) Source: https://context7.com/kuaidaili/python-sdk/llms.txt Manage your Kuaidaili account balance, create new proxy orders, retrieve order details, configure auto-renewal, and close orders. This snippet uses the Kuaidaili Python SDK with authentication credentials. ```python import kdl auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Get account balance balance = client.get_account_balance() print("Account balance:", balance) # Output: {'balance': '1580.50', 'currency': 'CNY'} # Create new order (auto-deduct from account balance) # product: product code from Kuaidaili # pay_type: 1=daily, 2=monthly, 3=yearly order_result = client.create_order( product='dps_basic', pay_type=2, sign_type='hmacsha1' ) print("Order created:", order_result) # Output: {'code': 0, 'msg': 'success', 'data': {'order_id': 'O123456789'}} # Get detailed order information order_info = client.get_order_info(sign_type='hmacsha1') print("Order info:", order_info) # Output: {'code': 0, 'data': {'order_id': '...', 'status': 'active', ...}} # Enable/disable auto-renewal # autorenew: 1=enable, 0=disable client.set_auto_renew(autorenew=1, sign_type='hmacsha1') print("Auto-renewal enabled") # Close order (only for post-paid orders) close_result = client.close_order(sign_type='hmacsha1') print("Order closed:", close_result) # Query available cities for dedicated proxies cities = client.query_kps_city(serie='kps_basic', sign_type='hmacsha1') print("Available cities:", cities) # Output: {'code': 0, 'data': {'cities': [{'name': '北京', 'count': 100}, ...]}} ``` -------------------------------- ### Set IP Whitelist for Dedicated Proxies Source: https://context7.com/kuaidaili/python-sdk/llms.txt Configures and retrieves the IP whitelist for dedicated proxy services. This ensures that only specified IP addresses can use the proxy. ```python client.set_ip_whitelist(["127.0.0.1", "192.168.0.139"]) whitelist = client.get_ip_whitelist() print("Whitelist:", whitelist) ``` -------------------------------- ### Kuaidaili Utility Functions (Python) Source: https://context7.com/kuaidaili/python-sdk/llms.txt Access utility functions provided by the Kuaidaili Python SDK, including generating random User-Agent strings and looking up area codes. These can enhance anonymity and provide useful data for your requests. ```python import kdl import requests auth = kdl.Auth("secret_id", "secret_key") client = kdl.Client(auth) # Get random User-Agent strings user_agents = client.get_ua(num=5, format='json') print("User agents:", user_agents) # Output: [ # 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...', # 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36...', # ... # ] # Get area code for specific location area_code = client.get_area_code(area='北京', format='json') print("Area code:", area_code) # Output: {'code': 0, 'data': {'area_code': '110000', 'area_name': '北京'}} # Use with proxy requests for better anonymity proxy = "45.67.89.12:8888" username = "user" password = "pass" ua = client.get_ua(num=1, format='json')[0] headers = { "User-Agent": ua, "Accept-Encoding": "Gzip" } proxies = { "http": f"http://{username}:{password}@{proxy}/", "https": f"http://{username}:{password}@{proxy}/" } response = requests.get( "https://httpbin.org/headers", proxies=proxies, headers=headers ) print("Response:", response.json()) # Output: {'headers': {'User-Agent': 'Mozilla/5.0...', ...}} ``` -------------------------------- ### Scrapy Proxy Rotation Middleware (Python) Source: https://context7.com/kuaidaili/python-sdk/llms.txt A custom Scrapy downloader middleware to automatically rotate proxies for requests. It randomly selects a proxy from a predefined list and formats it for inclusion in request metadata. Ensure this middleware is enabled in your Scrapy project's settings. ```python from scrapy import signals import random class ProxyDownloaderMiddleware: """ Custom Scrapy middleware for proxy rotation Add to DOWNLOADER_MIDDLEWARES in settings.py """ def process_request(self, request, spider): """ Called for each request before it's sent Randomly selects proxy from pool and configures authentication """ # Proxy list from external source (import or fetch dynamically) # from .myextend import pro # proxy = random.choice(pro.proxy_list) # Example static proxy list proxy_list = [ "45.67.89.12:8888", "23.45.67.89:8888", "98.76.54.32:8888" ] proxy = random.choice(proxy_list) # Username/password authentication username = "your_username" password = "your_password" request.meta['proxy'] = f"http://{username}:{password}@{proxy}/" # Whitelist authentication (simpler, requires IP whitelist setup) # request.meta['proxy'] = f"http://{proxy}/" return None # In settings.py, enable the middleware: # DOWNLOADER_MIDDLEWARES = { # 'tutorial.middlewares.ProxyDownloaderMiddleware': 543, # } # Example spider usage: # import scrapy # # class ExampleSpider(scrapy.Spider): # name = 'example' # start_urls = ['https://dev.kdlapi.com/testproxy'] # # def parse(self, response): # # All requests automatically use proxies from middleware # self.log(f'Response status: {response.status}') # yield {'content': response.text} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.