[ftputil] Setting a passive/active connection during the same connection
Marco Buccini
buccini.marco at alice.it
Mon Mar 24 11:01:27 CET 2008
Stefan Schwarzer ha scritto:
> Hi Marco,
>
> On 2008-03-23 20:37, Marco Buccini wrote:
>
>> Stefan Schwarzer ha scritto:
>>
>>> On 2008-03-23 17:48, Marco Buccini wrote:
>>> Note that the session_factory can by any callable, not
>>> necessarily a class. Thus, you could use
>>>
>>> def factory(host, user, pass, port):
>>> session = ftplib.FTP(host, user, pass, port)
>>> session.set_pasv(True)
>>> return session
>>>
>>> host = ftputil.FTPHost(host, user, pass, port,
>>> session_factory=factory)
>>>
>>> which is a bit shorter than using a class as the session
>>> factory.
>>>
>> I've already used this method, but I've found a little (big) problem.
>> Imagine you want to switch to active from passive or vice versa during
>> an established connection. You've to instanciate another FTPConnection
>> object, haven't you? So the previous connection is lost, and you've to
>> create a new connection. It' s not a good way .. :(
>>
>
> Is this an actual use case? If yes, could you describe the
> concrete application in more detail? Is it actually bad for
> performance (or otherwise) to open a new connection?
>
>
No, but I thought:
I estabilish a new connection object in passive mode. I do my work,
change directory, etc etc... Then I disconnect this object to connect in
active mode. I lose all my data (local directory, etc..)
>> For example, if you use linux and open a ftp command line client, you
>> can set passive/active at whatever time you want.
>>
>
> Again, I wonder whether this (changing at any time) is
> needed. I think, also in command line clients you _usually_
> change active/passive mode not more than once per
> connection.
>
>
If you try to connect, for example, to a host:
ftp upload.sourceforge.net
# you login
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
It's saying you that you are using active mode, and you've to set
"passive" (because sourceforge works in this way).
Than you set "passive" and it works well.
I can see that once you 've changed to passive mode, you can't return to
active mode. (maybe it depends on the server?)
>> What do you suggest me?
>>
>
> If you actually have an application which (according to
> measurements!) benefits from switching the mode on an
> established FTPHost instance please tell me. :-) You can
> compare the approaches by accessing FTPHost._session
> directly; just for tests, not production code, this should
> be ok.
>
>
No, I don't know any applications that do this.
So the user should switch only one time to passive/active mode.
> I can _imagine_ a use case for changing the mode of the
> session after establishing the connection: You try some
> commands, and if they fail, you retry after switching from
> active to passive (or vice versa). However, if you deal
> with a certain remote host, you will probably need this
> switching only one time and reuse the knowledge of active/
> passive mode for multiple connections. So the loss of
> reopening a single connection for dealing with - in the
> end - several connections to a server for some time
> probably isn't critical, only a bit inconvenient.
>
>
Infact I'm thinking of estabilishing only one connection at time.
Ok. I'll do so:
def factory(host, user, pass, port):
session = ftplib.FTP(host, user, pass, port)
session.set_pasv(True)
return session
host = ftputil.FTPHost(host, user, pass, port,
session_factory=factory)
It's a good method
Thank you!! :)
More information about the ftputil
mailing list