### Fetching a Proxy - Basic Usage - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md To fetch a random proxy using the `QuickProxy` class. ```python from swiftshadow import QuickProxy proxy = QuickProxy() print(proxy.as_string()) # Output: http://: ``` -------------------------------- ### Using swiftshadow QuickProxy with requests in Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Use Swiftshadow with/requests.md This snippet shows how to fetch a proxy using swiftshadow's QuickProxy class and then configure the requests library to use this proxy for an HTTP GET request. It requires both the swiftshadow and requests libraries to be installed. The output should be the IP address seen by the target server. ```python from swiftshadow import QuickProxy from requests import get # Fetch a proxy proxy = QuickProxy() # Use the proxy with requests resp = get('https://checkip.amazonaws.com', proxies=proxy.as_requests_dict()) print(resp.text) ``` -------------------------------- ### Manual Proxy Rotation - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Manually rotate proxies using the `rotate()` method of the `ProxyInterface`. This shows getting a proxy, then rotating and getting a new one. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface() # Get the first proxy print(swift.get().as_string()) # Rotate to a new proxy swift.rotate() print(swift.get().as_string()) ``` -------------------------------- ### Installing Swiftshadow Python Package Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/index.md Installs the Swiftshadow package using the pip package manager. This command downloads and installs the latest version of the library and its dependencies. ```bash pip install swiftshadow ``` -------------------------------- ### Using swiftshadow Proxy with requests - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Route requests through a proxy using the `requests` library by converting the `QuickProxy` object to a requests-compatible dictionary using `as_requests_dict()`. ```python from swiftshadow import QuickProxy from requests import get proxy = QuickProxy() resp = get('https://checkip.amazonaws.com', proxies=proxy.as_requests_dict()) print(resp.text) # Output: Proxy's IP address ``` -------------------------------- ### Install Swiftshadow Library - bash Source: https://github.com/sachin-sankar/swiftshadow/blob/main/README.md Installs the Swiftshadow Python library using the pip package manager. This command downloads and installs the latest version of the library from PyPI. ```bash pip install swiftshadow ``` -------------------------------- ### Filtering Proxies by Protocol - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Filter proxies by protocol (`http` or `https`) using `ProxyInterface`. This example fetches HTTPS proxies. ```python from swiftshadow.classes import ProxyInterface # Fetch HTTPS proxies swift = ProxyInterface(protocol="https") print(swift.get().as_string()) ``` -------------------------------- ### Using swiftshadow Proxy with httpx - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Route requests through a proxy using the `httpx` library by providing the proxy string from `as_string()` to the client's `proxies` parameter. ```python from swiftshadow import QuickProxy import httpx proxy = QuickProxy() with httpx.Client(proxies={"http://": proxy.as_string(), "https://": proxy.as_string()}) as client: resp = client.get('https://checkip.amazonaws.com') print(resp.text) # Output: Proxy's IP address ``` -------------------------------- ### Automatic Proxy Rotation - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Enable automatic rotation by setting `autoRotate=True` when initializing `ProxyInterface`. Each subsequent call to `get()` will return a new proxy. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface(autoRotate=True) # Each call to get() will return a new proxy print(swift.get().as_string()) print(swift.get().as_string()) ``` -------------------------------- ### Disabling Proxy Caching with QuickProxy - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md For one-off use cases where caching is not desired, use the `QuickProxy` class instead of `ProxyInterface`. ```python from swiftshadow import QuickProxy proxy = QuickProxy() print(proxy.as_string()) ``` -------------------------------- ### Combining Country and Protocol Filters - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Combine country and protocol filters for precise proxy selection using `ProxyInterface`. This example fetches HTTPS proxies from the US and India. ```python from swiftshadow.classes import ProxyInterface # Fetch HTTPS proxies from the US and India swift = ProxyInterface(countries=["US", "IN"], protocol="https") print(swift.get().as_string()) ``` -------------------------------- ### Filtering Proxies by Country - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Filter proxies by country using two-letter ISO codes. This example fetches proxies from the US and India using `ProxyInterface`. ```python from swiftshadow.classes import ProxyInterface # Fetch proxies from the US and India swift = ProxyInterface(countries=["US", "IN"]) print(swift.get().as_string()) ``` -------------------------------- ### Getting a Random HTTP Proxy with swiftshadow (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-Proxy-Class.md Initializes the ProxyInterface and retrieves a random HTTP proxy. The first call to ProxyInterface() triggers an update which may take time. The get() method returns a Proxy object, which is then converted to a string for printing. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface() print(swift.get().as_string()) ``` -------------------------------- ### Enabling Auto Proxy Rotation in swiftshadow (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-Proxy-Class.md Initializes the ProxyInterface with auto-rotation enabled. When autoRotate is True, calling the get() method automatically rotates to a new random proxy before returning it. This example shows two consecutive calls returning different proxies. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface(autoRotate=True) print(swift.get().as_string()) print(swift.get().as_string()) ``` -------------------------------- ### Get Random Proxy - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/README.md Demonstrates how to quickly fetch a random proxy string using the `QuickProxy` class from the swiftshadow library. It imports the class and prints the result. ```python from swiftshadow import QuickProxy print(QuickProxy()) # Output: http://: ``` -------------------------------- ### Custom Proxy Cache Folder - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Specify a custom cache folder (useful for environments like AWS Lambda) by providing the `cacheFolderPath` parameter to `ProxyInterface`. ```python from swiftshadow.classes import ProxyInterface # Use the /tmp directory for caching swift = ProxyInterface(cacheFolderPath="/tmp") print(swift.get().as_string()) ``` -------------------------------- ### Force Proxy Cache Update - swiftshadow - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/examples.md Force an update of the proxy cache using the `update()` method of the `ProxyInterface`. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface() swift.update() # Force update the proxy list print(swift.get().as_string()) ``` -------------------------------- ### Manually Rotating swiftshadow Proxies (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-Proxy-Class.md Initializes the ProxyInterface, gets and prints the current proxy, then manually rotates to a new random proxy from the available list and prints it. The rotate() method selects a new proxy without fetching new ones. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface() print(swift.get().as_string()) swift.rotate() print(swift.get().as_string()) ``` -------------------------------- ### Integrating SwiftShadow with Quart Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Usage-In-Async-Environments.md This snippet shows how to integrate SwiftShadow into a Quart application. It initializes `ProxyInterface` with both `autoUpdate=False` and `autoRotate=False`. A background task (`background_task`) is added using `app.add_background_task` in the `before_serving` handler to periodically call `swift.async_update()`. The root endpoint (`/`) manually calls `swift.rotate(validate_cache=False)` before getting and returning a proxy string to avoid potential event loop issues with auto-rotation in Quart. ```python from quart import Quart import asyncio from swiftshadow.classes import ProxyInterface app = Quart(__name__) swift = ProxyInterface(autoUpdate=False, autoRotate=False) # manual update and rotation async def background_task(): """Update proxies every 5 seconds.""" while True: print("Updating proxies...") await swift.async_update() await asyncio.sleep(5) @app.before_serving async def startup(): """Start the background task when the server starts.""" app.add_background_task(background_task) print("Background task started!") @app.route("/") async def home(): """Return a refreshed proxy.""" swift.rotate(validate_cache=False) # manually rotate without cache validation return "Hello, Quart! Here is a proxy: " + swift.get().as_string() if __name__ == "__main__": app.run(debug=True) ``` -------------------------------- ### Basic Usage of Swiftshadow QuickProxy and ProxyInterface Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/index.md Demonstrates how to quickly fetch a proxy using `QuickProxy` and how to use the more controlled `ProxyInterface` for fetching and automatically rotating proxies. It shows how to filter by country and protocol and print the proxy string representation. ```python from swiftshadow import QuickProxy from swiftshadow.classes import ProxyInterface # Get a proxy quickly proxy = QuickProxy(countries=["US"], protocol="http") print(proxy.as_string()) # Use ProxyInterface for more control proxy_manager = ProxyInterface(countries=["US"], protocol="http", autoRotate=True) print(proxy_manager.get().as_string()) ``` -------------------------------- ### Using QuickProxy for Proxy Retrieval (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-QuickProxy.md This snippet demonstrates how to use the `QuickProxy` function to fetch proxies. It shows fetching a default HTTP proxy and fetching an HTTPS proxy filtered by specific countries ('US', 'IN'). The `as_string()` method is used to display the proxy details. ```python from swiftshadow import QuickProxy # Get a random HTTP proxy proxy = QuickProxy() print(proxy.as_string()) # Get an HTTPS proxy from the US or India proxy = QuickProxy(countries=["US", "IN"], protocol="https") print(proxy.as_string()) ``` -------------------------------- ### Using swiftshadow with httpx (Synchronous) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Use Swiftshadow with/https.md Demonstrates how to fetch a proxy using QuickProxy and configure a synchronous httpx.Client to route HTTP/HTTPS requests through the fetched proxy. Requires the swiftshadow and httpx libraries. ```python from swiftshadow import QuickProxy import httpx # Fetch a proxy proxy = QuickProxy() # Use the proxy with httpx with httpx.Client(proxies={"http://": proxy.as_string(), "https://": proxy.as_string()}) as client: resp = client.get('https://checkip.amazonaws.com') print(resp.text) ``` -------------------------------- ### Using swiftshadow with httpx (Asynchronous) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Use Swiftshadow with/https.md Illustrates how to fetch a proxy using QuickProxy and configure an asynchronous httpx.AsyncClient to route HTTP/HTTPS requests through the fetched proxy. Requires swiftshadow, httpx, and asyncio. ```python from swiftshadow import QuickProxy import httpx import asyncio async def fetch_with_proxy(): # Fetch a proxy proxy = QuickProxy() # Use the proxy with httpx async with httpx.AsyncClient(proxies={"http://": proxy.as_string(), "https://": proxy.as_string()}) as client: resp = await client.get('https://checkip.amazonaws.com') print(resp.text) # Run the async function asyncio.run(fetch_with_proxy()) ``` -------------------------------- ### Integrating SwiftShadow with FastAPI Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Usage-In-Async-Environments.md This snippet demonstrates how to integrate SwiftShadow into a FastAPI application. It initializes `ProxyInterface` with `autoUpdate=False` and sets up a background task (`background_task`) using `asyncio.create_task` in the `startup_event` handler to periodically call `swift.async_update()`. The root endpoint (`/`) retrieves and returns a proxy string. ```python from fastapi import FastAPI import asyncio from swiftshadow.classes import ProxyInterface app = FastAPI() # Create the ProxyInterface with autoUpdate disabled. swift = ProxyInterface(autoUpdate=False, autoRotate=True) async def background_task(): """Update proxies every 5 seconds.""" while True: print("Updating proxies...") await swift.async_update() await asyncio.sleep(5) @app.on_event("startup") async def startup_event(): """Start the background task when the app starts.""" asyncio.create_task(background_task()) print("Background task started!") @app.get("/") async def home(): """Return a refreshed proxy.""" proxy = swift.get() return { "message": "Hello, FastAPI! Here is a proxy.", "proxy": proxy.as_string() } if __name__ == "__main__": import uvicorn uvicorn.run(app, host="127.0.0.1", port=8000, debug=True) ``` -------------------------------- ### Fetch Filtered Proxy - Python Source: https://github.com/sachin-sankar/swiftshadow/blob/main/README.md Shows how to use the `ProxyInterface` class for more controlled proxy fetching. It initializes the interface to filter for HTTPS proxies from the US and then retrieves and prints a proxy as a string. ```python from swiftshadow.classes import ProxyInterface # Fetch HTTPS proxies from the US swift = ProxyInterface(countries=["US"], protocol="https") print(swift.get().as_string()) # Output: https://: ``` -------------------------------- ### Filtering swiftshadow Proxies by Protocol (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-Proxy-Class.md Initializes the ProxyInterface, filtering the available proxies to include only those using the HTTPS protocol. By default, only HTTP proxies are included. Invalid protocols will result in no proxies being available. ```python swift = ProxyInterface(protocol='https') ``` -------------------------------- ### Using QuickProxy for No Caching in Lambda (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Use Swiftshadow with/AWS-Lambda.md If caching is not required or desired, the QuickProxy function can be used instead of ProxyInterface. QuickProxy does not implement caching, making it a suitable alternative for serverless environments like AWS Lambda where file system access might be restricted or caching is unnecessary. ```python from swiftshadow import QuickProxy proxy = QuickProxy() print(proxy.as_string()) ``` -------------------------------- ### Initializing ProxyInterface with Lambda Cache Path (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Use Swiftshadow with/AWS-Lambda.md To use ProxyInterface in AWS Lambda and enable caching despite the read-only file system, initialize the class by setting the 'cacheFolderPath' parameter to '/tmp'. The /tmp directory is writable in the Lambda environment, allowing the cache mechanism to function correctly. ```python from swiftshadow.classes import ProxyInterface swift = ProxyInterface(cacheFolderPath="/tmp") ``` -------------------------------- ### Forcing a swiftshadow Proxy Cache Update (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-Proxy-Class.md Initializes the ProxyInterface and then explicitly calls the update() method. This forces the library to refetch the proxy list from providers and refresh the internal cache, regardless of the cache expiration period. ```python swift = ProxyInterface() swift.update() # Force update the proxy list ``` -------------------------------- ### Filtering swiftshadow Proxies by Country (Python) Source: https://github.com/sachin-sankar/swiftshadow/blob/main/docs-src/Getting Started/Using-Proxy-Class.md Initializes the ProxyInterface, filtering the available proxies to include only those from the specified list of two-letter country codes (e.g., 'US', 'IN'). Invalid codes will result in no proxies being available. ```python swift = ProxyInterface(countries=['US', 'IN']) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.