【问题标题】:Python's mechanize proxy supportPython 的机械化代理支持
【发布时间】:2011-01-01 03:47:47
【问题描述】:

我对 python mechanize 的代理支持有疑问。我正在制作一些网络客户端脚本,我想在我的脚本中插入代理支持功能。

例如,如果我有:

params = urllib.urlencode({'id':id, 'passwd':pw})
rq = mechanize.Request('http://www.example.com', params) 
rs = mechanize.urlopen(rq)

如何将代理支持添加到我的机械化脚本中? 每当我打开这个www.example.com 网站时,我都希望它通过代理。

【问题讨论】:

    标签: python mechanize


    【解决方案1】:

    我不确定这是否有帮助,但您可以在 mechanize 代理浏览器上设置代理设置。

    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:password@myproxy.example.com: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")
    

    【讨论】:

    • 嗨,感谢您的回复,但那是 mechanize.browser 模块,这是我寻找的一些不同的方法,我正在寻找 mechanize .urlopen 方法谢谢
    • @harshal.c 那么有什么替代方案?
    【解决方案2】:

    您使用 mechanize.Request.set_proxy(host, type)(至少从 0.1.11 开始)

    假设一个 http 代理在 localhost:8888 运行

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

    应该可以。

    【讨论】:

    • 代理身份验证怎么样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多