FTP with urllib2 behind a proxy

Anand Pillai pythonguy at Hotpop.com
Wed Aug 13 12:37:37 CEST 2003


You need to install a proxyhandler, authhandler,
ftphandler and httphandler. Then build yourself
an opener, that opens the doors for you ... :-)

The following does the trick.

proxy_handler = urllib2.ProxyHandler( {'http': 'myhttpproxy:80',
                                       'https' : 'myhttpsproxy:443',
                                       'ftp'    : 'myftpproxy:21' } )

opener= urllib2.build_opener(proxy_handler, urllib2.HTTPBasicAuthHandler(),
                             urllib2.HTTPHandler, urllib2.HTTPSHandler,
                             urllib2.FTPHandler)

# install this opener
urllib2.install_opener(opener)

# Go ahead, knock knock!

req=urlli2.Request('ftp://ftp.gnu.org')
data=urllib2.urlopen(req).read()

Of course, replace the arbit proxy values I wrote
with your proxy values. If your proxy need authentication
, you will need to do a bit more here.

proxyauth='http://' + username + '@' + password + 'myproxy:myproxport'

Then your proxy handler becomes ( I am assuming a generic proxy
for all protocols here!)

proxy_handler = urllib2.ProxyHandler ( {'http' : proxyauth,
                                        'https' : proxyauth,
                                        'ftp' : proxyauth } )

HTH.

-Anand
                             

jjl at pobox.com (John J. Lee) wrote in message news:<87d6feykl8.fsf at pobox.com>...
> Zappelphillip at gmx.de (O. Koch) writes:
> 
> > Until now, i know that ftplib doesn't support proxies and that i have
> > to use urllib2. But i don't know how to use the urllib2 correct. I
> > found some examples, but i don't understand them.
> > 
> > Is there anyone who can help me?
> 
> import urllib2
> response = urllib2.urlopen("ftp://ftp.example.com/pub/myfile")
> data = response.read()
> response.close()
> 
> 
> Does that do the trick?
> 
> 
> John




More information about the Python-list mailing list