FTP example going through a FTP Proxy

jakecjacobson jakecjacobson at gmail.com
Wed Jan 7 15:56:58 EST 2009


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)



More information about the Python-list mailing list