The neurotic.datasets.ftpauth module implements a urllib.request-compatible FTP handler that prompts for and remembers passwords.

class neurotic.datasets.ftpauth.FTPBasicAuthHandler(password_mgr=None)[source]

This subclass of urllib.request.FTPHandler implements basic authentication management for FTP connections. Like urllib.request.HTTPBasicAuthHandler, this handler for FTP connections has a password manager that it checks for login credentials before connecting to a server.

This subclass also ensures that file size is included in the response header, which can fail for some FTP servers if the original FTPHandler is used.

This handler can be installed globally in a Python session so that calls to urllib.request.urlopen('ftp://...') will use it automatically:

>>> handler = FTPBasicAuthHandler()
>>> handler.add_password(None, uri, user, passwd)  # realm must be None
>>> opener = urllib.request.build_opener(handler)
>>> urllib.request.install_opener(opener)

Install neurotic.datasets.ftpauth.FTPBasicAuthHandler as the global default FTP handler.

Note that urllib.request.install_opener() used here will remove all other non-default handlers installed in a different opener, such as an urllib.request.HTTPBasicAuthHandler.