Python 2.5 script to connect to FTP and download f

2019-01-26 02:20发布

I am sure this has been resolved before but I cannot seem to find a similar Q&A (newbie) Using Windows XP and Python 2.5, I m trying to use a script to connect to an FTP server and dowload files. It should be simple but following the instructions of similar scripts I get the errors:

ftp.login('USERNAME')
  File "C:\Python25\lib\ftplib.py", line 373, in login
    if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
  File "C:\Python25\lib\ftplib.py", line 241, in sendcmd
    return self.getresp()
  File "C:\Python25\lib\ftplib.py", line 216, in getresp
    raise error_perm, resp
error_perm: 530 User USERNAME cannot log in.

The script I use is:

def handleDownload(block):
    file.write(block)
    print ".",

# Create an instance of the FTP object
# FTP('hostname', 'username', 'password')
ftp = FTP('servername')

print 'ftplib example'
# Log in to the server
print 'Logging in.'
# You can specify username and password here if you like:
ftp.login('USERNAME', 'password') 
#print ftp.login()

# This is the directory 
directory = '/GIS/test/data'
# Change to that directory.  
print 'Changing to ' + directory
ftp.cwd(directory)

# Print the contents of the directory
ftp.retrlines('LIST')

I appreciate this might be a trivial question, but if anyone can provide some insights it would be very helpful!

Thanks, S

标签: python ftp
3条回答
趁早两清
2楼-- · 2019-01-26 02:36

I can't understand which library are you using. Python standard urllib2 is sufficient:

import urllib2, shutil

ftpfile = urllib2.urlopen("ftp://host.example.com/path/to/file")
localfile = open("/tmp/downloaded", "wb")
shutil.copyfileobj(ftpfile, localfile)

If you need to login (anonymous login isn't sufficient), then specify the credentials inside the url:

urllib2.urlopen("ftp://user:password@host.example.com/rest/of/the/url")
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-26 02:37

the first trivial check would be to open an interactive session (i.e. ftp yourself to this server with the same credentials), to be sure that this is not a permission issue..

Another source of failure, you might need to give your username as domain\username when connecting to a MS ftp server.

Maybe that helps ?

查看更多
迷人小祖宗
4楼-- · 2019-01-26 02:48
ftp.login('USERNAME', 'password') 

Replace this with real data. According to the error you are trying to login as "USERNAME" with the password "password" which obviously won't work.

Also, replace servername in ftp = FTP('servername') with the hostname of the server you want to connect to.

查看更多
登录 后发表回答