[twill] changing http_proxy from within Python

Titus Brown titus at caltech.edu
Wed Oct 11 23:52:32 PDT 2006

-> I know twill honors the http_proxy variable, but for a web-testing script I'm creating I need it to
-> use different proxies in the same run, that is, change http_proxy while the script is running. I'm
-> running twill from inside python (very easy, excellent piece of software), and all my attempts at
-> modifying http_proxy are not being honored by twill. It only "sees" the pre-run value of http_proxy.
-> Any ideas?

Hi, Arturo,

I think I see the problem: the function 'urllib.getproxies()' is called
once, when the Browser object is initialized.  (See
twill/other_packages/mechanize/_auth.py, line 99, from the latest twill

The simplest thing to do is simply reset the browser.  If that's too
expensive for you, or otherwise messes you up by trashing browser state,
you can always swap out the proxy handler.  For that, you would need
to do something *like*

	for i, h in enumerate(browser._browser.handlers):
	   if isinstance(h, mechanize._urllib2.ProxyHandler):
	      browser._browser.handlers[i] = mechanize._urllib2.ProxyHandler()

I haven't tested this approach, though.


More information about the twill mailing list