Setting http proxy programmatically

Anand B Pillai abpillai at lycos.com
Wed Mar 19 09:41:49 EST 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.
        httpproxy="firewall.server.gateway:someport1"
        ftpproxy="firewall.server.gateway:someport2"
        sslproxy="firewall.server.gateway:someport3"

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

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


        #Build your opener
        #First create proxyhandler
        proxy_support=urllib2.ProxyHandler({"http":httpproxystring,
                                            "https":sslproxystring,
                                            "ftp":ftpproxystring})

        authinfo=urllib2.HTTPBasicAuthHandler()
        opener=urllib2.build_opener(proxy_support, authinfo,
urllib2.HTTPHandler)
        #install opener
        urllib2.install_opener(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
your
urllib2.urlopen() calls go through the installed opener, so your job
is
done.

Best Regards,

Anand Pillai

Python scripts         -> http://members.fortunecity.com/anandpillai
ASPN                   -> aspn.activestate.com
WWF                    -> http://passport.panda.org

Davor Cengija <dcengija_remove_ at inet.hr> wrote in message news:<13615821.zngrY5C3n6 at lothar.cengija.com>...
> 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.




More information about the Python-list mailing list