diff -r e9e7eca7efee -r bf71ee032bb4 src/eric7/EricNetwork/EricFtp.py --- a/src/eric7/EricNetwork/EricFtp.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/EricNetwork/EricFtp.py Wed Jul 13 14:55:47 2022 +0200 @@ -9,14 +9,14 @@ """ import enum -import ftplib # secok +import ftplib # secok from socket import _GLOBAL_DEFAULT_TIMEOUT class EricFtpProxyError(ftplib.Error): """ Class to signal an error related to proxy configuration. - + The error message starts with a three digit error code followed by a space and the error string. Supported error codes are: <ul> @@ -37,6 +37,7 @@ <li>992: proxy usage is enabled but no proxy password given</li> </ul> """ + pass @@ -44,16 +45,17 @@ """ Class defining the supported FTP proxy types. """ - NO_PROXY = 0 # no proxy - NON_AUTHORIZING = 1 # non authorizing proxy - USER_SERVER = 2 # proxy login first, than user@remote.host - SITE = 3 # proxy login first, than use SITE command - OPEN = 4 # proxy login first, than use OPEN command - USER_PROXYUSER_SERVER = 5 # one login for both + + NO_PROXY = 0 # no proxy + NON_AUTHORIZING = 1 # non authorizing proxy + USER_SERVER = 2 # proxy login first, than user@remote.host + SITE = 3 # proxy login first, than use SITE command + OPEN = 4 # proxy login first, than use OPEN command + USER_PROXYUSER_SERVER = 5 # one login for both PROXYUSER_SERVER = 6 # proxy login with remote host given, than normal remote login AUTH_RESP = 7 # authenticate to proxy with AUTH and RESP commands - BLUECOAT = 8 # bluecoat proxy + BLUECOAT = 8 # bluecoat proxy class EricFtp(ftplib.FTP): @@ -61,13 +63,24 @@ Class implementing an extension to the Python FTP class to support FTP proxies. """ - def __init__(self, host="", user="", password="", acct="", # secok - proxyType=EricFtpProxyType.NO_PROXY, proxyHost="", - proxyPort=ftplib.FTP_PORT, proxyUser="", proxyPassword="", - proxyAccount="", timeout=_GLOBAL_DEFAULT_TIMEOUT): + + def __init__( + self, + host="", + user="", + password="", + acct="", # secok + proxyType=EricFtpProxyType.NO_PROXY, + proxyHost="", + proxyPort=ftplib.FTP_PORT, + proxyUser="", + proxyPassword="", + proxyAccount="", + timeout=_GLOBAL_DEFAULT_TIMEOUT, + ): """ Constructor - + @param host name of the FTP host @type str @param user user name for login to FTP host @@ -92,33 +105,39 @@ @type int """ super().__init__() - + self.__timeout = timeout - + self.__proxyType = proxyType self.__proxyHost = proxyHost self.__proxyPort = proxyPort self.__proxyUser = proxyUser self.__proxyPassword = proxyPassword self.__proxyAccount = proxyAccount - + self.__host = host self.__port = ftplib.FTP_PORT self.__user = user self.__password = password self.__acct = acct - + if host: self.connect(host) if user: self.login(user, password, acct) - - def setProxy(self, proxyType=EricFtpProxyType.NO_PROXY, proxyHost="", - proxyPort=ftplib.FTP_PORT, proxyUser="", proxyPassword="", - proxyAccount=""): + + def setProxy( + self, + proxyType=EricFtpProxyType.NO_PROXY, + proxyHost="", + proxyPort=ftplib.FTP_PORT, + proxyUser="", + proxyPassword="", + proxyAccount="", + ): """ Public method to set the proxy configuration. - + @param proxyType type of the FTP proxy @type EricFtpProxyType @param proxyHost name of the FTP proxy @@ -138,12 +157,11 @@ self.__proxyUser = proxyUser self.__proxyPassword = proxyPassword self.__proxyAccount = proxyAccount - - def setProxyAuthentication(self, proxyUser="", proxyPassword="", - proxyAccount=""): + + def setProxyAuthentication(self, proxyUser="", proxyPassword="", proxyAccount=""): """ Public method to set the proxy authentication info. - + @param proxyUser user name for login to the proxy @type str @param proxyPassword password for login to the proxy @@ -154,15 +172,15 @@ self.__proxyUser = proxyUser self.__proxyPassword = proxyPassword self.__proxyAccount = proxyAccount - + def connect(self, host="", port=0, timeout=-999): """ Public method to connect to the given FTP server. - + This extended method connects to the proxy instead of the given host, if a proxy is to be used. It throws an exception, if the proxy data is incomplete. - + @param host name of the FTP host @type str @param port port of the FTP host @@ -179,27 +197,26 @@ self.__port = port if timeout != -999: self.__timeout = timeout - + if self.__proxyType != EricFtpProxyType.NO_PROXY: if not self.__proxyHost: raise EricFtpProxyError( - "990 Proxy usage requested, but no proxy host given.") - - return super().connect( - self.__proxyHost, self.__proxyPort, self.__timeout) + "990 Proxy usage requested, but no proxy host given." + ) + + return super().connect(self.__proxyHost, self.__proxyPort, self.__timeout) else: - return super().connect( - self.__host, self.__port, self.__timeout) - - def login(self, user="", password="", acct=""): # secok + return super().connect(self.__host, self.__port, self.__timeout) + + def login(self, user="", password="", acct=""): # secok """ Public method to login to the FTP server. - + This extended method respects the FTP proxy configuration. There are many different FTP proxy products available. But unfortunately there is no standard for how o traverse a FTP proxy. The lis below shows the sequence of commands used. - + <table> <tr><td>user</td><td>Username for remote host</td></tr> <tr><td>pass</td><td>Password for remote host</td></tr> @@ -208,7 +225,7 @@ <tr><td>remote.host</td><td>Hostname of the remote FTP server</td> </tr> </table> - + <dl> <dt>EricFtpProxyType.NO_PROXY:</dt> <dd> @@ -269,7 +286,7 @@ ACCT prpass </dd> </dl> - + @param user username for the remote host @type str @param password password for the remote host @@ -285,34 +302,36 @@ user = "anonymous" if not password: # make sure it is a string - password = "" # secok + password = "" # secok if not acct: # make sure it is a string acct = "" - if user == "anonymous" and password in {'', '-'}: + if user == "anonymous" and password in {"", "-"}: password += "anonymous@" - + if self.__proxyType != EricFtpProxyType.NO_PROXY: if self.__proxyType != EricFtpProxyType.NON_AUTHORIZING: # check, if a valid proxy configuration is known if not self.__proxyUser: raise EricFtpProxyError( - "991 Proxy usage requested, but no proxy user given") + "991 Proxy usage requested, but no proxy user given" + ) if not self.__proxyPassword: raise EricFtpProxyError( - "992 Proxy usage requested, but no proxy password" - " given") - - if self.__proxyType in [EricFtpProxyType.NON_AUTHORIZING, - EricFtpProxyType.AUTH_RESP, - EricFtpProxyType.BLUECOAT]: + "992 Proxy usage requested, but no proxy password" " given" + ) + + if self.__proxyType in [ + EricFtpProxyType.NON_AUTHORIZING, + EricFtpProxyType.AUTH_RESP, + EricFtpProxyType.BLUECOAT, + ]: user += "@" + self.__host if self.__proxyType == EricFtpProxyType.BLUECOAT: user += " " + self.__proxyUser acct = self.__proxyPassword elif self.__proxyType == EricFtpProxyType.USER_PROXYUSER_SERVER: - user = "{0}@{1}@{2}".format( - user, self.__proxyUser, self.__host) + user = "{0}@{1}@{2}".format(user, self.__proxyUser, self.__host) password = "{0}@{1}".format(password, self.__proxyPassword) else: pruser = self.__proxyUser @@ -320,7 +339,7 @@ user += "@" + self.__host elif self.__proxyType == EricFtpProxyType.PROXYUSER_SERVER: pruser += "@" + self.__host - + # authenticate to the proxy first presp = self.sendcmd("USER " + pruser) if presp[0] == "3": @@ -329,24 +348,28 @@ presp = self.sendcmd("ACCT " + self.__proxyAccount) if presp[0] != "2": raise EricFtpProxyError( - "9{0}0 Error authorizing at proxy\n{1}".format( - presp[0], presp)) - + "9{0}0 Error authorizing at proxy\n{1}".format(presp[0], presp) + ) + if self.__proxyType == EricFtpProxyType.SITE: # send SITE command presp = self.sendcmd("SITE " + self.__host) if presp[0] != "2": raise EricFtpProxyError( "9{0}0 Error sending SITE command\n{1}".format( - presp[0], presp)) + presp[0], presp + ) + ) elif self.__proxyType == EricFtpProxyType.OPEN: # send OPEN command presp = self.sendcmd("OPEN " + self.__host) if presp[0] != "2": raise EricFtpProxyError( "9{0}0 Error sending OPEN command\n{1}".format( - presp[0], presp)) - + presp[0], presp + ) + ) + # authenticate to the remote host or combined to proxy and remote host resp = self.sendcmd("USER " + user) if resp[0] == "3": @@ -354,8 +377,8 @@ if resp[0] == "3": resp = self.sendcmd("ACCT " + acct) if resp[0] != "2": - raise ftplib.error_reply(resp) # secok - + raise ftplib.error_reply(resp) # secok + if self.__proxyType == EricFtpProxyType.AUTH_RESP: # authorize to the FTP proxy presp = self.sendcmd("AUTH " + self.__proxyUser) @@ -363,7 +386,7 @@ presp = self.sendcmd("RESP " + self.__proxyPassword) if presp[0] != "2": raise EricFtpProxyError( - "9{0}0 Error authorizing at proxy\n{1}".format( - presp[0], presp)) - + "9{0}0 Error authorizing at proxy\n{1}".format(presp[0], presp) + ) + return resp