0

I would like to test code which connects to a ftp server as clients and up- and downloads files.

Is there a Python library which provides a context manager which runs a ftp server on localhost (port 2121)?

I would like the usage of this library look roughly like this:

def test_my_ftp_foo(self):
    with ftp_server_context(user='myuser', password='mypassword', port=2121):
        with ftputil.FTPHost(host='localhost', 'myuser', 'mypassword', port=2121) as host:
            host.makedirs('a/b')
            host.chrdir(...')
            ...

I see several ways to implement this library.

One would be to use subprocess.Popen() and run a script using pyftpdlib until __exit__() tells the context manager to kill the ftp server.

0

You can start an FTP server from the command line by using the twisted python framework.

Once you have installed twisted, and possibly pywin32, with pip you can start an ftp server from the command line, (or using popen).

The command to start one is:

python -m twisted ftp 

You can specify authorization, port, root directory, etc. note that this is not a production grade FTP server

0

I wrote a simple library which provides what I was looking for:

https://github.com/tbz-pariv/ftpservercontext

FTP server context manager for testing ftp client code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.