Possibility to specify port in __init__ of ftplib.FTP

Hi. If there is possible to connect and login to ftp server within __init__, I think it would make sense to add parameter **port** in ftplib.FTP. It could look like this: ``` def __init__(self, host='', port=0, user='', passwd='', acct='', timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None, *, encoding='utf-8'): self.encoding = encoding self.source_address = source_address self.timeout = timeout if host: self.connect(host, port) if user: self.login(user, passwd, acct) ``` (Or port parameter could be added at the end of the __init__ parameters not to break current logic.) Currently if I need to specify port, I have to do it like this: ``` with FTP() as ftp: ftp.connect(host, port) ftp.login(user, password) # my actions ``` If the port parameter would be added, I could use it like this: ``` with FTP(host, port, user, password) as ftp: # my actions ``` In all my cases, all I need is to connect to ftp server and work with it. If there is some trouble with connection or login, the code still can't continue working with ftp... Do you rather use connect() and login() explicitly or just let it connect and login within __init__ implicitly? What do you think about it? Thanks.

Marty writes:
If there is possible to connect and login to ftp server within __init__, I think it would make sense to add parameter **port** in ftplib.FTP. It could look like this: ``` def __init__(self, host='', port=0, user='', passwd='', acct='', timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None, *, encoding='utf-8'): [...] (Or port parameter could be added at the end of the __init__ parameters not to break current logic.)
I think that last comment is right. In calls you can put it in the natural (host, port, user, password) order using keywords. Another possibility would be to overload 'host' to accept 'HOST:PORT' or even 'USER:PASSWORD@HOST:PORT'.
Currently if I need to specify port, I have to do it like this: ``` with FTP() as ftp: ftp.connect(host, port) ftp.login(user, password) # my actions
To be honest, I really don't think that's so burdensome. In your own code, you could just wrap it in your own class that DTRTs, using any or all of the above extensions: with MyFTP('example.com', 8042, 'me', 'apocryphum') as ftp: # my actions with MyFTP('example.com:8042', 'me', 'apocryphum') as ftp: # my actions with MyFTP('me:apocryphum@example.com:8042') as ftp: # my actions
participants (2)
-
Marty
-
Stephen J. Turnbull