Search code examples
pythonproxyurllib2mechanizemechanize-python

Proxy seems to be ignored by Mechanize?


I am using an http proxy and the Mechanize module. I initialize the mechanize object and set the proxy like so:

self.br = mechanize.Browser()
self.br.set_proxies({"http": proxyAddress})   #proxy address is like 1.1.1.1:8080

Then I open the site like so:

response = self.br.open("http://google.com")

My problem is that mechanize seems to be completely ignoring the proxy. If I debug and inspect the br object, under the proxy handler I can see my proxy settings. However, even if I give a bad proxy Mechanize just goes about its business like I never set a proxy. What gives?

edit: I have also tried:

mechanize.install_opener(mechanize.build_opener(mechanize.ProxyHandler({'http': "127.0.0.1:99"})))
response = mechanize.urlopen("http://google.com")

And it seems to be ignoring my proxy as well. (I didn't even give it a valid proxy, shouldn't it fail on a URLError?)


Solution

  • Figured it out after talking on the email list:

    import mechanize
    browser = mechanize.Browser()
    browser.set_proxies(proxies={"http": "myproxy.example.com:1234"},
                    proxy_bypass=lambda hostname: False)