[Python] Getting 502 Bad Gateway Error with LunaProxy and Selenium Wire

Discussão em 'Python' iniciado por Stack, Junho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I hope you're doing well. I'm trying to automate form submissions on "https://securelastexpense.com" using a Python script with Selenium Wire and LunaProxy, but I'm encountering a 502 Bad Gateway error. The error appears to be related to a TLS/SSL handshake failure, as shown in the browser logs (e.g., TlsProtocolException('Cannot establish TLS with securelastexpense.com:443...')). I've tried updating the cryptography library and setting verify_ssl=False in Selenium Wire options, but the issue persists.

    I’d appreciate any help to resolve this. Thank you!

    [Error Code Screen Shot][1]

    import random
    from seleniumwire import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from webdriver_manager.chrome import ChromeDriverManager
    import logging

    # Set up basic logging
    logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

    # LunaProxy credentials (placeholders)
    LUNAPROXY_USERNAME_BASE = "lu5715465"
    LUNAPROXY_PASSWORD = "k3fd84569H"
    LUNAPROXY_HOST = "pr.lunaproxy.com"
    LUNAPROXY_PORT = "12233"

    # Generate a sample proxy username
    def get_lunaproxy_username(city="losangeles", state="ca"):
    return f"user-{LUNAPROXY_USERNAME_BASE}-region-us-st-{state}-city-{city}"

    # Test proxy connection (simplified)
    def test_proxy(proxy_url):
    try:
    import requests
    test_url = "http://myip.lunaproxy.io"
    response = requests.get(test_url, proxies={"http": proxy_url, "https": proxy_url}, timeout=10, verify=False)
    if response.status_code == 200:
    logging.info("Proxy test successful")
    return True
    logging.error(f"Proxy test failed: {response.status_code}")
    return False
    except Exception as e:
    logging.error(f"Proxy test failed: {e}")
    return False

    def main():
    proxy_username = get_lunaproxy_username()
    proxy_url = f"http://{proxy_username}:{LUNAPROXY_PASSWORD}@{LUNAPROXY_HOST}:{LUNAPROXY_PORT}"
    logging.info(f"Using proxy: {proxy_url}")

    # Test proxy
    if not test_proxy(proxy_url):
    logging.warning("Proxy test failed, proceeding without proxy")
    seleniumwire_options = {}
    else:
    seleniumwire_options = {
    "proxy": {
    "http": proxy_url,
    "https": proxy_url,
    "no_proxy": "localhost,127.0.0.1"
    },
    "verify_ssl": False
    }

    # Set up Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--log-level=3")

    # Initialize driver
    driver = webdriver.Chrome(
    service=Service(ChromeDriverManager().install()),
    options=chrome_options,
    seleniumwire_options=seleniumwire_options
    )

    try:
    # Load the target page
    driver.get("https://securelastexpense.com")
    WebDriverWait(driver, 20).until(
    EC.presence_of_element_located((By.ID, "scroll-anchor"))
    )
    logging.info("Page loaded successfully")
    except Exception as e:
    logging.error(f"Failed to load page: {e}")
    driver.save_screenshot("error_screenshot.png")
    finally:
    driver.quit()

    if __name__ == "__main__":
    main()```


    [1]: https://i.sstatic.net/KWs5qgGy.png

    Continue reading...

Compartilhe esta Página