paul
paul

Reputation: 325

Python's mechanize proxy support

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.

Upvotes: 9

Views: 20215

Answers (2)

user302043
user302043

Reputation:

You use mechanize.Request.set_proxy(host, type) (at least as of 0.1.11)

assuming an http proxy running at localhost:8888

req = mechanize.Request("http://www.google.com")
req.set_proxy("localhost:8888","http")
mechanize.urlopen(req)

Should work.

Upvotes: 9

fulmicoton
fulmicoton

Reputation: 15968

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")

Upvotes: 32

Related Questions