【问题标题】:Python mechanize doesn't work when HTTPS and Proxy Authentication required需要 HTTPS 和代理身份验证时,Python 机械化不起作用
【发布时间】:2012-11-10 10:01:28
【问题描述】:

我使用 Python 2.7.2 和 Mechanize 0.2.5。
当我访问 Internet 时,我必须通过代理服务器。我写了下面的代码,但是最后一行出现了一个 URLError.. 有没有人对此有任何解决方案?

import mechanize

br = mechanize.Browser()
br.set_debug_http(True)
br.set_handle_robots(False)

br.set_proxies({
    "http"  : "192.168.20.130:8080",
    "https" : "192.168.20.130:8080",})
br.add_proxy_password("username", "password")

br.open("http://www.google.co.jp/")  # OK
br.open("https://www.google.co.jp/") # Proxy Authentication Required

【问题讨论】:

    标签: python https proxy mechanize


    【解决方案1】:

    我不建议你使用 Mechanize,它已经过时了。看看requests它会 让您的生活更轻松。对请求使用代理就是这样:

    import requests
    
    proxies = {
      "http": "10.10.1.10:3128",
      "https": "10.10.1.10:1080",
    }
    
    requests.get("http://example.org", proxies=proxies)
    

    【讨论】:

    • 非常感谢。我不知道请求模块。我现在正在尝试。如何为代理身份验证指定用户名和密码?
    • 您需要使用代理网址,例如:username:mypassword@10.10.1.10:3128
    • 谢谢。基本身份验证肯定会接受您的方式。当代理服务器需要摘要认证时,用户名和密码不能嵌入代理 URL。我尝试了 requests.auth.HTTPProxyAuth,但代理返回 407。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2010-12-01
    • 2016-04-24
    • 2021-10-10
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多