tags:

views:

554

answers:

1

I need to write some python ftp code that uses a ftp proxy. The proxy doesn't require authentication but the ftp server I am connecting to does. I have the following code but I am getting a "I/O error(ftp error): 501 USER format: proxy-user:auth-method@destination. Closing connection." error. My code is:

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 would be grateful for any assistance I can get.

+1  A: 

I use the following code block which seems similar except i include the protocol in the top_level_url I use (and of course it's http).

You might also try calling install_opener after each build_opener call and then using urllib2.urlopen

auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='RESTRICTED ACCESS',
                          uri='http://website.com',
                          user='username',
                          passwd='password')
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
urllib2.urlopen('http://website.com/....')
Jehiah
Thanks, I also need to go through a firewall proxy and I think that is what is causing my error. The proxy doesn't need authentication just the ftp server I am connecting to.
I saw you had a proxyhandler setup properly, did using both your openers and `install_opener` along with specifying 'ftp://' for the uri help?
Jehiah