代理似乎被 Mechanize 忽略了?

发布于 2024-10-12 21:05:02 字数 665 浏览 4 评论 0原文

我正在使用 http 代理和 Mechanize 模块。我初始化 mechanize 对象并设置代理,如下所示:

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

然后我像这样打开站点:

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

我的问题是 mechanize 似乎完全忽略了代理。如果我调试并检查 br 对象,在代理处理程序下我可以看到我的代理设置。然而,即使我提供了一个糟糕的代理,Mechanize 也会像我从未设置过代理一样继续其业务。什么给?

编辑: 我也尝试过:

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

它似乎也忽略了我的代理。 (我什至没有给它一个有效的代理,它不应该因 URLError 而失败吗?)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-10-19 21:05:02

在讨论电子邮件列表后弄清楚了:

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

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)
贩梦商人 2024-10-19 21:05:02

如果您尝试访问 https 站点,请将代理设置为 https,如下所示
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"})

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"})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文