Monthly Archives: February 2012

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