Last active
February 17, 2022 16:10
-
-
Save derac/fde8646a686cea09f02438d2054f94c1 to your computer and use it in GitHub Desktop.
per-context proxies with Playwright
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from playwright.async_api import async_playwright | |
proxy_servers = [ | |
"http://PROXY_IP1:PORT", | |
"http://PROXY_IP2:PORT", | |
] | |
async def spawn_context(browser, proxy_server): | |
context = await browser.new_context(proxy={"server": proxy_server}) | |
page = await context.new_page() | |
await page.goto("https://www.icanhazip.com/") | |
await page.wait_for_timeout(1000 * 100) | |
async def main(): | |
async with async_playwright() as playwright: | |
browser = await playwright.chromium.launch( | |
headless=False, | |
# From Playwright docs, workaround for Windows issue. | |
proxy={"server": "http://per-context"}, | |
) | |
tasks = [] | |
for proxy_server in proxy_servers: | |
tasks.append(asyncio.create_task(spawn_context(browser, proxy_server))) | |
await asyncio.gather(*tasks) | |
await browser.close() | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment