Python's mechanize proxy support

♀尐吖头ヾ 提交于 2019-11-27 05:41:10

问题


I have a question about python mechanize's proxy support. I'm making some web client script, and I would like to insert proxy support function into my script.

For example, if I have:

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

How can I add proxy support into my mechanize script? Whenever I open this www.example.com website, i would like it to go through the proxy.


回答1:


You use mechanize.Request.set_proxy(host, type) (at least as of 0.1.11)

assuming an http proxy running at localhost:8888

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

Should work.




回答2:


I'm not sure whether that help or not but you can set proxy settings on mechanize proxy browser.

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


来源:https://stackoverflow.com/questions/1997894/pythons-mechanize-proxy-support

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!