FTP transfert

A

Antoine Logean

Hi,

given this code:


from ftplib import FTP

connection = FTP('ftp.python.org')
connection.login()
connection.dir()
connection.close()

The script write on the standart output a list of files and directories
present on the server. Good. But how can I have this output in a string
that I can work with ? sys.stdout is an open file object, is'nt it ?
So if I do :

string_out = sys.stdout.readlines()

the script of course freeze. (what is for me logic)

How can I solve the problem ?

Why connection.sendcmd('ls') does not work ? the same for
connection.sendcmd('LIST') ? 'ls' is a valid ftp command. In the RFC of
FTP the commands are differents as the one I use every day : no get,
mget, put, mput, ... but LIST, RETR, ... what is the difference ?

Thanks for your help

Antoine
 
P

Peter Otten

Antoine said:
from ftplib import FTP

connection = FTP('ftp.python.org')
connection.login()
connection.dir()
connection.close()

The script write on the standart output a list of files and directories
present on the server. Good. But how can I have this output in a string
that I can work with ? sys.stdout is an open file object, is'nt it ?

Seems like dir() accepts a callback that takes one argument, so you need not
mess with stdout. E. g.:

from ftplib import FTP

connection = FTP('ftp.python.org')
connection.login()
lines = []
connection.dir(lines.append)
connection.close()

print "\n".join(lines)

Peter
 
T

Tor Iver Wilhelmsen

Antoine Logean said:
connection.dir()

Check the docs for this method; it can take an argument, a callback
function. You can probably have this function build the representation
you want.
 
E

Eddie Corns

Antoine Logean said:
Why connection.sendcmd('ls') does not work ? the same for
connection.sendcmd('LIST') ? 'ls' is a valid ftp command. In the RFC of
FTP the commands are differents as the one I use every day : no get,
mget, put, mput, ... but LIST, RETR, ... what is the difference ?

The RFC commands constitute the protocol between FTP servers/clients and are
specified (reasonably) exactly to get good interworking between different
implementations. The dir, ls, get etc commands constitute the user interface
and are modelled on what a user on that system might expect to know. The
Python library, unusually for Python, does not do much in the way of hiding
the protocol level commands. Perhaps there are some 3rd party addons to add a
more user friendly level, the vaults of Parnassus
(http://www.vex.net/parnassus/) seems to have some promising libraries.

Eddie
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top