Setting http proxy programmatically

Davor Cengija dcengija_remove_ at
Wed Mar 19 11:58:37 CET 2003

I read about urllib2.Request and urllib2.ProxyHandler, but being really new 
in Python, I don't know how to use it (btw, an example in the documentation 
would be really helpful).

Basically, I need to set http proxy within my program. I use 
urllib2.urlopen(url, data) to submit some form data and it works fine when 
having no proxy, or when http_proxy environment variable is set, but the 
program will be run behind a firewall which forces proxy.

Here's what I have now (simplified):

    def addEmail(self, emailValue):
        """Adds the email which will be submitted to the form"""
        log(("Adding", emailValue))[self.emailField] = emailValue
        self.params = urllib.urlencode(

    # GET method
    def doSubmit(self, email):
        preparedUrl = self.url + "?%s" % self.params
        return urllib2.urlopen(preparedUrl).info()

    # POST method
    def doSubmit(self, email):
        log((self.url, self.params))
        return urllib2.urlopen(self.url, self.params).info() is a simple dictionary object, and self.params is that 
dictionary, but urlencoded.

Where I should instantiate ProxyHandler and/or Request and how can I force 
urllib2.urlopen() to use it?

Davor Cengija, dcengija_remove_ at

More information about the Python-list mailing list