Setting http proxy programmatically

Davor Cengija dcengija_remove_ at inet.hr
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.data[self.emailField] = emailValue
        self.params = urllib.urlencode(self.data)

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

    # POST method
    def doSubmit(self, email):
        self.addEmail(email)
        log((self.url, self.params))
        return urllib2.urlopen(self.url, self.params).info()

self.data 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?

Thanks.
-- 
Davor Cengija, dcengija_remove_ at inet.hr




More information about the Python-list mailing list