Python's mechanize proxy support

paul picture paul · Jan 4, 2010 · Viewed 18.4k times · Source

I have a question about python mechanize's proxy support. I'm making some web client script, and I would like to insert proxy support function into my script.

For example, if I have:

params = urllib.urlencode({'id':id, 'passwd':pw})
rq = mechanize.Request('http://www.example.com', params) 
rs = mechanize.urlopen(rq)

How can I add proxy support into my mechanize script? Whenever I open this www.example.com website, i would like it to go through the proxy.

Answer

fulmicoton picture fulmicoton · Jan 4, 2010

I'm not sure whether that help or not but you can set proxy settings on mechanize proxy browser.

br = Browser()
# Explicitly configure proxies (Browser will attempt to set good defaults).
# Note the userinfo ("joe:password@") and port number (":3128") are optional.
br.set_proxies({"http": "joe:[email protected]:3128",
                "ftp": "proxy.example.com",
                })
# Add HTTP Basic/Digest auth username and password for HTTP proxy access.
# (equivalent to using "joe:password@..." form above)
br.add_proxy_password("joe", "password")