FTPlib

H

Harlin Seritt

Using ftplib from Python I am trying to get all files in a particular
directory using ftplib and then send those same files to another ftp
server. I have tried using commands like 'get *' and 'mget *' with no
success.

I am using the following:

srcFtp = FTP(srcHost)
srcFtp.login(srcUser, srcPass)
srcDir = srcFtp.nlst('.')
for file in srcDir:
print file[2:]
srcFtp.transfercmd('get '+file[2:])

I've verified that I do have a connection with the ftp server and the
files as 'file[2:]' are indeed the file names.

Anyone know why I get the following error?

podcast-1.mp3
Traceback (most recent call last):
File "podsync.py", line 17, in ?
srcFtp.transfercmd('get '+file[2:])
File "C:\Python24\lib\ftplib.py", line 345, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python24\lib\ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python24\lib\ftplib.py", line 241, in sendcmd
return self.getresp()
File "C:\Python24\lib\ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 500 Syntax error, command unrecognized.

Thanks,

Harlin Seritt
 
F

Fredrik Lundh

Harlin said:
Using ftplib from Python I am trying to get all files in a particular
directory using ftplib and then send those same files to another ftp
server. I have tried using commands like 'get *' and 'mget *' with no
success.

I am using the following:

srcFtp = FTP(srcHost)
srcFtp.login(srcUser, srcPass)
srcDir = srcFtp.nlst('.')
for file in srcDir:
print file[2:]
srcFtp.transfercmd('get '+file[2:])

I've verified that I do have a connection with the ftp server and the
files as 'file[2:]' are indeed the file names.

Anyone know why I get the following error?

podcast-1.mp3
Traceback (most recent call last):
File "podsync.py", line 17, in ?
srcFtp.transfercmd('get '+file[2:])
File "C:\Python24\lib\ftplib.py", line 345, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python24\lib\ftplib.py", line 327, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python24\lib\ftplib.py", line 241, in sendcmd
return self.getresp()
File "C:\Python24\lib\ftplib.py", line 216, in getresp
raise error_perm, resp
ftplib.error_perm: 500 Syntax error, command unrecognized.

ftplib works with FTP protocol commands, not ftp client commands (such
as get and mget).

to read data from a remote server, you can use something like:

dst = open(localfile, "wb")
srcFtp.retrbinary("RETR " + file[2:], dst.write)
dst.close()

for more examples, see

http://effbot.org/librarybook/ftplib.htm

for an extensive description of FTP-as-deployed, see daniel bernstein's
site:

http://cr.yp.to/ftp.html

hope this helps!

</F>
 
P

Peter A. Schott

Shouldn't that be:

srcFtp.retrbinary('get ' + file[2:])

or some similar variation?

You're also missing a block telling this where to write the local file.

LocalFile = file.open(file[2:], "wb")
LocalFile.write(srcFtp.retrbinary('get ' + file[2:]) )

(or something similar)

-Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top