Setting http proxy programmatically

Anand B Pillai abpillai at
Wed Mar 19 15:41:49 CET 2003

Hi Davor

   For networking with proxies/authenticated proxies urllib2 provides
the handler "ProxyHandler". You need to create your own ProxyHandler
and create an urllib2 "opener" using it.

 I am also behind a proxy/firewall, an authenticated one. Here is how
I get my python app to work around it.

 def InitNetwork(self):

        #you need to find out the name of your http/https/ftp proxy
        #Ask your sys admin about it.

        #if your proxy is authenicated
        if authenticated: 
           #build proxy strings using username/password
           httpproxystring='http://' +  username + ':' + Password +
'@' + httpproxy
           sslproxystring='http://' + username + ':' + Password + '@'
+ sslproxy
           ftpproxystring='http://' + username + ':' + Password + '@'
+ ftpproxy

           #build proxy strings without authentication
           httpproxystring = 'http://' +  httpproxy
           sslproxystring = 'http://' + sslproxy
           ftpproxystring = 'http://' + ftpproxy

        #Build your opener
        #First create proxyhandler

        opener=urllib2.build_opener(proxy_support, authinfo,
        #install opener

  Create a function as above with your proxy values and call it before
you use any urllib2 functions to get data from the internet. Now all
urllib2.urlopen() calls go through the installed opener, so your job

Best Regards,

Anand Pillai

Python scripts         ->
ASPN                   ->
WWF                    ->

Davor Cengija <dcengija_remove_ at> wrote in message news:<13615821.zngrY5C3n6 at>...
> 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):
>         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()
> 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.

More information about the Python-list mailing list