Question

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

Was it helpful?

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)

OTHER TIPS

If if you are trying to access https site then set proxy as https as follow br = mechanize.Browser()

    # Cookie Jar
    cj = cookielib.LWPCookieJar()
    br.set_cookiejar(cj)

    # Browser options
    br.set_handle_equiv(True)
    br.set_handle_gzip(True)
    br.set_handle_redirect(True)
    br.set_handle_referer(True)
    br.set_handle_robots(False)

    # Follows refresh 0 but not hangs on refresh > 0
    br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

    # Want debugging messages?
    #br.set_debug_http(True)
    #br.set_debug_redirects(True)
    #br.set_debug_responses(True)

    # User-Agent (this is cheating, ok?)
    br.addheaders = [('User-agent', 'Mozilla/4.0 (Compatible; MSIE 8.0; Windows NT 5.2; Trident/6.0)')]

    br.set_proxies({"https": "XXX.XX.246.56:33835"})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top