using ftplib

J

John Salerno

I'm experimenting with this now and I'm a little confused about
transferring commands. This might be more of an FTP question than
strictly Python, but it's still related to how to use the ftplib methods.

Anyway, if what I want to do is send a command to change the file
permission settings of a file (or files), I assume I would use
transfercmd() and the command would be something like SITE CHMOD 755
name.py, for example.

What I'm confused about is why the documentation says to send a PORT or
PASV command as well. Is this just something that must be done before
each command?

Also, in order to figure out what the FTP commands are to begin with, I
did a little testing in FileZilla and I noted what was being displayed
at the top (things like PORT xxx, STOR xxx, SITE CHMOD, etc. depending
on what I was doing). So I assume these are the 'cmd' parameter. So here
are a couple of questions:

1. Are PORT/PASV necessary to start a transfer?

2. Is the cmd parameter some kind of list, or is it just a string and
you have to call a separate transfercmd() for each command?

3. My burning question: also during every transfer I made in FileZilla,
I see that it sometimes sends a TYPE A or TYPE I command as well. What
are these, and are they also necessary when I'm using transfercmd()?

Thanks!
John
 
D

Dave Hughes

John said:
I'm experimenting with this now and I'm a little confused about
transferring commands. This might be more of an FTP question than
strictly Python, but it's still related to how to use the ftplib
methods.

Anyway, if what I want to do is send a command to change the file
permission settings of a file (or files), I assume I would use
transfercmd() and the command would be something like SITE CHMOD 755
name.py, for example.

What I'm confused about is why the documentation says to send a PORT
or PASV command as well. Is this just something that must be done
before each command?

Also, in order to figure out what the FTP commands are to begin with,
I did a little testing in FileZilla and I noted what was being
displayed at the top (things like PORT xxx, STOR xxx, SITE CHMOD,
etc. depending on what I was doing). So I assume these are the 'cmd'
parameter. So here are a couple of questions:

1. Are PORT/PASV necessary to start a transfer?

Indeed. FTP is a tricky (and _very_ old) protocol that does things in a
very different manner to the later (simpler) protocols like HTTP. Not
sure how familiar you are with FTP, so my apologies if I wind up
repeating stuff you know already:

FTP sessions typically have *two* connections: the "control"
connection, and the "data" connection. The control connection is from
the client to the server (typically port 21 on the server side), and is
used to send commands to the server and obtain responses.

The PORT command tells the server to open a data connection to the
client (yes, that's not a typo: from the server to the client) and the
parameters tell the server the address and port to connect to. This is
the "active" (or default) mode for FTP (although due to the heavy use
of NAT these days, "passive" mode is probably as, or more common in
practice).

The PASV command is the reverse of the PORT command: it requests the
server listen on a new port for a data connection from the client to
the server.

Data is never transferred over the control connection, it always goes
over the separate data connection, hence why PORT or PASV are required
before initiating a transfer.

See RFC 959 for the full spec (gawd, it's been a long time since I read
that ... amazed I still remember the number):
http://www.faqs.org/rfcs/rfc959.html
2. Is the cmd parameter some kind of list, or is it just a string and
you have to call a separate transfercmd() for each command?

Sorry, I haven't used ftplib yet, but a brief glance at the code
suggests to me that it's just a string and you probably do need to call
a separate transfercmd for each command.
3. My burning question: also during every transfer I made in
FileZilla, I see that it sometimes sends a TYPE A or TYPE I command
as well. What are these, and are they also necessary when I'm using
transfercmd()?

TYPE I indicates that an "Image" transfer is to take place (though this
is more commonly referred to nowadays as a "binary" transfer). In an
binary transfer the data is transferred verbatim with no
transformations.

TYPE A indicates that an ASCII transfer is to take place. An ASCII
transfer is intended for use with text files and indicates that the
data should be transformed from the native text format on the client
platform to the native text format on the server platform. For example,
transferring a file from a DOS/Windows client to a UNIX/Linux platform
in ASCII mode would convert CRLF line endings (ASCII char 13 + ASCII
char 10) to LF line endings (ASCII char 10).

There are other transfer modes as well, though I forget exactly what
they are (there's probably one for EBCDIC <shudder> :).

Take a look at the storbinary, storlines, retrbinary and retrlines
methods of the FTP object: looks like they perform the appropriate TYPE
command for you, then pass the specified command to transfercmd.


HTH,

Dave.
--
 
J

John Salerno

Dave said:
Indeed. FTP is a tricky (and _very_ old) protocol that does things in a
very different manner to the later (simpler) protocols like HTTP. Not
sure how familiar you are with FTP, so my apologies if I wind up
repeating stuff you know already:

Thanks very much, all that information was great to have.
TYPE A indicates that an ASCII transfer is to take place. An ASCII
transfer is intended for use with text files and indicates that the
data should be transformed from the native text format on the client
platform to the native text format on the server platform. For example,
transferring a file from a DOS/Windows client to a UNIX/Linux platform
in ASCII mode would convert CRLF line endings (ASCII char 13 + ASCII
char 10) to LF line endings (ASCII char 10).

There are other transfer modes as well, though I forget exactly what
they are (there's probably one for EBCDIC <shudder> :).

Take a look at the storbinary, storlines, retrbinary and retrlines
methods of the FTP object: looks like they perform the appropriate TYPE
command for you, then pass the specified command to transfercmd.

So if I already have files on the server and I want to change file
permissions, do I need to mess with TYPE A/TYPE I commands, or are those
strictly for when you *transfer* files? Because I just did a quick test
of changing file permissions through my FTP program, and it still sent a
TYPE A command in the process. I know that the retr and stor methods
might do this automatically, but if I don't need to use those, and just
need to use transfercmd (assuming this *is* the right method to use when
changing file permissions), do I need to manually send a TYPE A/I
command as well?
 
S

Steve Holden

John Salerno wrote:
[...]
So if I already have files on the server and I want to change file
permissions, do I need to mess with TYPE A/TYPE I commands, or are those
strictly for when you *transfer* files? Because I just did a quick test
of changing file permissions through my FTP program, and it still sent a
TYPE A command in the process. I know that the retr and stor methods
might do this automatically, but if I don't need to use those, and just
need to use transfercmd (assuming this *is* the right method to use when
changing file permissions), do I need to manually send a TYPE A/I
command as well?

The answer is "probably not", but a better answer is "try it in the
interactive interpreter and see".

regards
Steve
 
D

Dave Hughes

Steve said:
John Salerno wrote:
[...]
So if I already have files on the server and I want to change file
permissions, do I need to mess with TYPE A/TYPE I commands, or are
those strictly for when you transfer files? Because I just did a
quick test of changing file permissions through my FTP program,
and it still sent a TYPE A command in the process. I know that the
retr and stor methods might do this automatically, but if I don't
need to use those, and just need to use transfercmd (assuming this
is the right method to use when changing file permissions), do I
need to manually send a TYPE A/I command as well?

The answer is "probably not", but a better answer is "try it in the
interactive interpreter and see".

As far as I recall your speculation is indeed correct: a TYPE command
is only necessary before a transfer (it's only used to transform data
passing over the secondary data connection). I'm reasonably certain you
don't need *anything* before a SITE command (like SITE CHMOD), not even
PORT or PASV (unless the specific SITE command being invoked requires a
data transfer, which CHMOD shouldn't). But as Steve suggests, the best
way to know for sure is to give it a whirl and see what happens :)

Incidentally, you might like to experiment with the command line ftp
client. On both Linux and Windows, the "quote" command can be used to
send commands "raw" to the FTP server. For example:

ftp> quote SITE CHMOD 755 afile.py


Dave.

--
 
J

John Salerno

Dave said:
As far as I recall your speculation is indeed correct: a TYPE command
is only necessary before a transfer (it's only used to transform data
passing over the secondary data connection). I'm reasonably certain you
don't need *anything* before a SITE command (like SITE CHMOD), not even
PORT or PASV (unless the specific SITE command being invoked requires a
data transfer, which CHMOD shouldn't). But as Steve suggests, the best
way to know for sure is to give it a whirl and see what happens :)

Thanks. I sometimes forget I can experiment with the interactive
interpreter, but I also didn't want to screw up anything on my server
space by doing something weird! :)
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top