An FTP client class. To create a connection, call the class using these arguments: host, user, passwd, acct, timeout, source_address, encoding The first four arguments are all strings, and have default value ''. The parameter ´timeout´ must be numeric and defaults to None i
| 72 | |
| 73 | # The class itself |
| 74 | class FTP: |
| 75 | '''An FTP client class. |
| 76 | |
| 77 | To create a connection, call the class using these arguments: |
| 78 | host, user, passwd, acct, timeout, source_address, encoding |
| 79 | |
| 80 | The first four arguments are all strings, and have default value ''. |
| 81 | The parameter ´timeout´ must be numeric and defaults to None if not |
| 82 | passed, meaning that no timeout will be set on any ftp socket(s). |
| 83 | If a timeout is passed, then this is now the default timeout for all ftp |
| 84 | socket operations for this instance. |
| 85 | The last parameter is the encoding of filenames, which defaults to utf-8. |
| 86 | |
| 87 | Then use self.connect() with optional host and port argument. |
| 88 | |
| 89 | To download a file, use ftp.retrlines('RETR ' + filename), |
| 90 | or ftp.retrbinary() with slightly different arguments. |
| 91 | To upload a file, use ftp.storlines() or ftp.storbinary(), |
| 92 | which have an open file as argument (see their definitions |
| 93 | below for details). |
| 94 | The download/upload functions first issue appropriate TYPE |
| 95 | and PORT or PASV commands. |
| 96 | ''' |
| 97 | |
| 98 | debugging = 0 |
| 99 | host = '' |
| 100 | port = FTP_PORT |
| 101 | maxline = MAXLINE |
| 102 | sock = None |
| 103 | file = None |
| 104 | welcome = None |
| 105 | passiveserver = True |
| 106 | # Disables https://bugs.python.org/issue43285 security if set to True. |
| 107 | trust_server_pasv_ipv4_address = False |
| 108 | |
| 109 | def __init__(self, host='', user='', passwd='', acct='', |
| 110 | timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None, *, |
| 111 | encoding='utf-8'): |
| 112 | """Initialization method (called by class instantiation). |
| 113 | Initialize host to localhost, port to standard ftp port. |
| 114 | Optional arguments are host (for connect()), |
| 115 | and user, passwd, acct (for login()). |
| 116 | """ |
| 117 | self.encoding = encoding |
| 118 | self.source_address = source_address |
| 119 | self.timeout = timeout |
| 120 | if host: |
| 121 | self.connect(host) |
| 122 | if user: |
| 123 | self.login(user, passwd, acct) |
| 124 | |
| 125 | def __enter__(self): |
| 126 | return self |
| 127 | |
| 128 | # Context management protocol: try to quit() if active |
| 129 | def __exit__(self, *args): |
| 130 | if self.sock is not None: |
| 131 | try: |