FTP example going through a FTP Proxy

jakecjacobson jakecjacobson at gmail.com
Thu Jan 8 11:44:26 EST 2009


On Jan 7, 3:56 pm, jakecjacobson <jakecjacob... at gmail.com> wrote:
> On Jan 7, 2:11 pm, jakecjacobson <jakecjacob... at gmail.com> wrote:
>
>
>
> > On Jan 7, 12:32 pm, jakecjacobson <jakecjacob... at gmail.com> wrote:
>
> > > Hi,
>
> > > I need to write a simple Python script that I can connect to a FTP
> > > server and download files from the server to my local box.  I am
> > > required to go through a FTP Proxy and I don't see any examples on how
> > > to do this.  The FTP proxy doesn't require username or password to
> > > connect but the FTP server that I am connecting to does.
>
> > > Any examples on how to do this would be greatly appreciated.  I am
> > > limited to using Python version 2.4.3 on a Linux box.
>
> > This is what I have tried so far,
>
> > import urllib
>
> > proxies = {'ftp':'ftp://proxy_server:21'}
> > ftp_server = 'ftp.somecompany.com'
> > ftp_port='21'
> > username = 'aaaa'
> > password = 'secretPW'
>
> > ftp_string='ftp://' + username + '@' + password + ftp_server + ':' +
> > ftp_port
>
> > data = urllib.urlopen(ftp_string, proxies=proxies)
>
> > data=urllib.urlopen(req).read()
>
> > print data
>
> > I get the following error:
>
> > Traceback (most recent call last):
> >   File "./ftptest.py", line 22, in ?
> >     data = urllib.urlopen(ftp_server, proxies=proxies)
> >   File "/usr/lib/python2.4/urllib.py", line 82, in urlopen
> >     return opener.open(url)
> >   File "/usr/lib/python2.4/urllib.py", line 190, in open
> >     return getattr(self, name)(url)
> >   File "/usr/lib/python2.4/urllib.py", line 470, in open_ftp
> >     host, path = splithost(url)
> >   File "/usr/lib/python2.4/urllib.py", line 949, in splithost
> >     match = _hostprog.match(url)
> > TypeError: expected string or buffer
>
> I might be getting closer.  Now I am getting "I/O error(ftp error):
> (111, 'Connection refused')" error with the following code:
>
> import urllib2
>
> proxies = {'ftp':'ftp://proxy_server:21'}
> ftp_server = 'ftp.somecompany.com'
> ftp_port='21'
> username = 'aaaa'
> password = 'secretPW'
>
> password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
> top_level_url = ftp_server
> password_mgr.add_password(None, top_level_url, username, password)
>
> proxy_support = urllib2.ProxyHandler(proxies)
> handler = urllib2.HTTPBasicAuthHandler(password_mgr)
> opener = urllib2.build_opener(proxy_support)
> opener = urllib2.build_opener(handler)
> a_url = 'ftp://' + ftp_server + ':' + ftp_port + '/'
> print a_url
>
> try:
>         data = opener.open(a_url)
>         print data
> except IOError, (errno, strerror):
>         print "I/O error(%s): %s" % (errno, strerror)

I tried the same code from a different box and got a different error
message:

I/O error(ftp error): 501 USER format: proxy-user:auth-
method at destination.  Closing connection.

My guess is that my original box couldn't connect with the firewall
proxy so I was getting a connection refused error.  Now it appears
that the password mgr has an issue if I understand the error
correctly.  I really hope that someone out in the Python Community can
give me a pointer.



More information about the Python-list mailing list