Pyppeteer on GCP VM Instance Not Detecting Network Requests

25 Views Asked by At

I'm using Pyppeteer on a GCP VM instance to monitor network requests from a specific website. The code works perfectly on my local machine, capturing all the network traffic as expected. However, when I run the same script on the GCP instance, it fails to detect any network traffic.

Here's a snippet of the code I'm using:

async def monitor_network(url, date, episode_title):
    retry_count = 0  
    browser = None  
    print(f"{url}")

    while retry_count < MAX_RETRIES:
        try:
            browser = await launch(headless=True)
            page = await browser.newPage()
            version_info = await browser.version()
            print(f'Browser version: {version_info}')

            async def handle_request(req):
                print(req.url)
                await req.continue_()
            await page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36")

            await page.setRequestInterception(True)
            page.on('requestfailed', lambda req: print(
                f'Request failed: {req.url}'))
            page.on('request', lambda req: asyncio.ensure_future(
                handle_request(req)))
            await page.goto(url, waitUntil='networkidle0', timeout=180000)


            break 

        except Exception as e:
            print(f"An error occurred: {e}")
            retry_count += 1  
            print(f"Retrying... ({retry_count}/{MAX_RETRIES})")
            await asyncio.sleep(2)  

        finally:
            if browser:  
                await browser.close()


# Some codes omitted

Things I've tried:

Checked the firewall rules on GCP - all outbound traffic is allowed. Tried different User-Agents. Made sure the Pyppeteer version is the same as on my local machine.

Why is the network detection working on my local machine but not on the GCP instance? Any help or insights would be greatly appreciated.

This does not mean that all networks cannot be detected.

0

There are 0 best solutions below