[ftputil] server question
Stefan Schwarzer
sschwarzer at sschwarzer.net
Thu Jun 17 20:26:58 MEST 2004
On Sun, 2004-06-20 12:57:53 -0500, tyson wrote:
> Im having trouble using the ftputil module to connect to a local
> server. I am using wx Python and want to transfer somes files from my
> c:\ on a remote computer to one of our local server. When I try to
> connect to the server, I get an error. This is how my code looks
>
> import ftputil
> import ftplib
> host = ftputil.FTPHost('test.vltool.com', 'user', 'password', 5432)
>
> After I try this I get an error stating:
> None (10061, 'Connection Refused')
>
> Is there anything you can suggest that might help??
ftputil.FTPHost passes all its positional and keyword arguments to the
session_factory (which is ftplib.FTP by default). Only the keyword
argument session_factory is handled specially.
It seems you have passed in the last argument assuming that it is a
port number. However, the Python documentation says that the fourth
positional argument of ftplib.FTP's constructor is 'acct'. If you want
to instantiate the FTP session with another port use the recipe from
the ftputil documentation:
"""
FTPHost objects
Construction
FTPHost instances may be generated with the following call:
host = ftputil.FTPHost(host, user, password, account,
session_factory=ftplib.FTP)
The first four parameters are strings with the same meaning as for the FTP
class in the ftplib module. The keyword argument session_factory may be used to
generate FTP connections with other factories than the default ftplib.FTP. For
example, the M2Crypto distribution uses a secure FTP class which is derived
from ftplib.FTP.
In fact, all positional and keyword arguments other than session_factory are
passed to the factory to generate a new background session (which happens for
every remote file that is opened; see below).
This functionality of the constructor also allows to wrap ftplib.FTP objects to
do something that wouldn't be possible with the ftplib.FTP constructor alone.
As an example, assume you want to connect to another than the default port but
ftplib.FTP only offers this by means of its connect method, but not via its
constructor. The solution is to provide a wrapper class:
import ftplib
import ftputil
EXAMPLE_PORT = 50001
class MySession(ftplib.FTP):
def __init__(self, host, userid, password, port):
"""Act like ftplib.FTP's constructor but connect to other port."""
ftplib.FTP.__init__(self)
self.connect(host, port)
self.login(userid, password)
# try not to use MySession() as factory, - use the class itself
host = ftputil.FTPHost(host, userid, password,
port=EXAMPLE_PORT, session_factory=MySession)
# use `host` as usual
On login, the format of the directory listings (needed for stat'ing files and
directories) should be determined automatically. If not, you may use the method
set_directory_format to set the format "manually".
"""
Stefan
More information about the ftputil
mailing list