import mechanicalsoup
import requests

session = requests.Session()
session.trust_env = False
session.proxies.update({
    "http": "http://proxy.example.net:8080",
    "https": "http://proxy.example.net:8080",
})

browser = mechanicalsoup.StatefulBrowser(session=session)

Choose the endpoint before opening the form page. For a public form where public proxies are permitted, it can be a candidate from the HTTP proxy list. Build the session and pass it to StatefulBrowser as shown. Use that same browser for open, form selection and submit_selected; close it with browser.close() when the workflow ends.

This keeps the proxy policy and cookie state on the session used for the whole exchange. A proxies argument supplied to one initial fetch does not automatically configure a later submission.

trust_env = False makes this example's route independent of Requests' environment proxy settings. It also disables environment-based CA bundle selection and .netrc authentication. Supply those explicitly if the workflow needs them, or account for the environment when choosing a different policy.

متعلقہ مضامین