Python FTP Server and Client Minimal Test

On the server side, I use pyftpdlib. The path assigned to the authorizer must exist. Server address is localhost. Port number cannot be 21 or 80.

from pyftpdlib import ftpserver

def main():
    authorizer = ftpserver.DummyAuthorizer()
    authorizer.add_anonymous("/somewhere/somedir")
    handler = ftpserver.FTPHandler
    handler.authorizer = authorizer
    address = ('127.0.0.1', 7152)
    ftpd = ftpserver.FTPServer(address, handler)
    ftpd.serve_forever()


if __name__ == '__main__':
    main()

When the server.py is running. Execute the client side. Make sure it connects to the right address and port. Retrieve test.zip stored in the authorized path, then extract it to the place where client.py is started.

from ftplib import FTP
import zipfile

def main():
    ftp = FTP()
    ftp.connect('127.0.0.1','7152')  
    ftp.login() 
    ftp.retrbinary('RETR test.zip', open('test.zip', 'wb').write)
    ftp.quit()
    
    f = zipfile.ZipFile('test.zip', 'a')
    f.extractall()

if __name__ == '__main__':
    main()
Advertisements

Leave a comment

Filed under Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s