| | 131 | Construction |
|---|
| | 132 | ------------ |
|---|
| | 133 | |
|---|
| | 134 | FTPHost instances may be generated with the following call: |
|---|
| | 135 | |
|---|
| | 136 | host = ftputil.FTPHost(host, user, password, account, |
|---|
| | 137 | session_factory=ftplib.FTP) |
|---|
| | 138 | |
|---|
| | 139 | The first four parameters are strings with the same meaning as for the |
|---|
| | 140 | FTP class in the ftplib module. The keyword argument session_factory |
|---|
| | 141 | may be used to generate FTP connections with other factories than the |
|---|
| | 142 | default ftplib.FTP. For example, the M2Crypto distribution uses a |
|---|
| | 143 | secure FTP class which is derived from ftplib.FTP. |
|---|
| | 144 | |
|---|
| | 145 | In fact, all positional and keyword arguments other than |
|---|
| | 146 | session_factory are passed to the factory to generate a new background |
|---|
| | 147 | session (which happens for every remote file that is opened; see |
|---|
| | 148 | below). |
|---|
| | 149 | |
|---|
| | 150 | FTPHost attributes and methods |
|---|
| | 151 | ------------------------------ |
|---|
| | 152 | |
|---|
| | 153 | curdir, pardir, sep |
|---|
| | 154 | are strings which denote the current and the parent directory on |
|---|
| | 155 | the remote server. sep identifies the path separator. Though RFC |
|---|
| | 156 | 959 (File Transfer Protocol) notes that these values may be server |
|---|
| | 157 | dependent, the Unix counterparts seem to work well in practice, |
|---|
| | 158 | even for non-Unix servers. |
|---|
| | 159 | |
|---|
| | 160 | file(path, mode='r') |
|---|
| | 161 | returns a file-like object that is connected to the path on the |
|---|
| | 162 | remote host. This path may be absolute or relative to current |
|---|
| | 163 | directory on the remote host (this directory can be determined |
|---|
| | 164 | with the getcwd method). As with local file objects the default |
|---|
| | 165 | mode is 'r', i. e. reading text files. Valid modes are 'r', 'rb', |
|---|
| | 166 | 'w', and 'wb'. |
|---|
| | 167 | |
|---|
| | 168 | open(path, mode='r') |
|---|
| | 169 | is an alias for file (see above). |
|---|
| | 170 | |
|---|
| | 171 | copyfileobj(source, target, length=64*1024) |
|---|
| | 172 | copies the contents from the file-like object source to the |
|---|
| | 173 | file-like object target. The only difference to shutil.copyfileobj |
|---|
| | 174 | is the default buffer size. |
|---|
| | 175 | |
|---|
| | 176 | upload(source, target, mode='') |
|---|
| | 177 | copies a local source file (given by a filename, i. e. a string) |
|---|
| | 178 | to the remote host under the name target. Both source and target |
|---|
| | 179 | may be absolute paths or relative to their corresponding current |
|---|
| | 180 | directory (on the local or the remote host, respectively). The |
|---|
| | 181 | mode may be '' or 'a' for ASCII uploads or 'b' for binary uploads. |
|---|
| | 182 | ASCII mode is the default (again, similar to regular local file |
|---|
| | 183 | objects). |
|---|
| | 184 | |
|---|
| | 185 | download(source, target, mode='') |
|---|
| | 186 | performs a download from the remote source to a target file. Both |
|---|
| | 187 | source and target are strings. Additionally, the description of |
|---|
| | 188 | the upload method applies here, too. |
|---|
| | 189 | |
|---|
| | 190 | upload_if_newer(source, target, mode='') |
|---|
| | 191 | is similar to the upload method. The only difference is that the |
|---|
| | 192 | upload is only invoked if the time of the last modification for |
|---|
| | 193 | the source file is more recent than that of the target file, or |
|---|
| | 194 | the target doesn't exist at all. |
|---|
| | 195 | |
|---|
| | 196 | download_if_newer(source, target, mode='') |
|---|
| | 197 | corresponds to upload_if_newer but performs a download from the |
|---|
| | 198 | server to the local host. Read the descriptions of download and |
|---|
| | 199 | upload_if_newer for more. |
|---|
| | 200 | |
|---|
| | 201 | close() |
|---|
| | 202 | closes the connection to the remote host. After this, no more |
|---|
| | 203 | interaction with the FTP server is possible without using a new |
|---|
| | 204 | FTPHost object. |
|---|
| | 205 | |
|---|
| | 206 | getcwd() |
|---|
| | 207 | returns the absolute current directory on the remote host. This |
|---|
| | 208 | method acts similar to os.getcwd. |
|---|
| | 209 | |
|---|
| | 210 | chdir(directory) |
|---|
| | 211 | sets the current directory on the FTP server. This resembles |
|---|
| | 212 | os.chdir, as you may have expected. :-) |
|---|
| | 213 | |
|---|
| | 214 | mkdir(path, [mode]) |
|---|
| | 215 | makes the given directory on the remote host. In the current |
|---|
| | 216 | implementation, this doesn't construct "intermediate" directories |
|---|
| | 217 | which don't already exist. The mode parameter is ignored. This is |
|---|
| | 218 | for compatibilty with os.mkdir if an FTPHost object is passed into |
|---|
| | 219 | a function instead of the os module (see the subsection on Python |
|---|
| | 220 | exceptions above for an explanation). |
|---|
| | 221 | |
|---|
| | 222 | rmdir(path) |
|---|
| | 223 | removes the given remote directory. Currently, "intermediate" |
|---|
| | 224 | directories can't be deleted (as with os.rmdir). |
|---|
| | 225 | |
|---|
| | 226 | remove(path) |
|---|
| | 227 | removes a file on the remote host (similar to os.remove). |
|---|
| | 228 | |
|---|
| | 229 | unlink(path) |
|---|
| | 230 | is an alias for remove. |
|---|
| | 231 | |
|---|
| | 232 | rename(source, target) |
|---|
| | 233 | renames the source file (or directory) on the FTP server. |
|---|
| | 234 | |
|---|
| | 235 | listdir(path) |
|---|
| | 236 | returns a list containing the names of the files and directories |
|---|
| | 237 | in the given path; similar to os.listdir. |
|---|
| | 238 | |
|---|
| | 239 | lstat(path) |
|---|
| | 240 | returns an object similar that from os.lstat (a "tuple" with |
|---|
| | 241 | additional attributes; see the documentation of the os module for |
|---|
| | 242 | details). However, due to the nature of the application, there are |
|---|
| | 243 | some important aspects to keep in mind: |
|---|
| | 244 | |
|---|
| | 245 | - The result is derived by parsing the output of a DIR command on |
|---|
| | 246 | the server. Therefore, the result from FTPHost.lstat can not |
|---|
| | 247 | contain nore information than the received text. In particular: |
|---|
| | 248 | |
|---|
| | 249 | - User and group ids can only be determined as strings, not as |
|---|
| | 250 | numbers, and that only, if the server supplies them. This is |
|---|
| | 251 | usually the case with Unix servers but may not be for other FTP |
|---|
| | 252 | server programs. |
|---|
| | 253 | |
|---|
| | 254 | - Values for the time of the last modification may be rough, |
|---|
| | 255 | depending on the information from the server. For timestamps |
|---|
| | 256 | older than a year, this usually means that the precision of the |
|---|
| | 257 | modification timestamp value is not better than days. For newer |
|---|
| | 258 | files, the information may be accurate to a minute. |
|---|
| | 259 | |
|---|
| | 260 | - Links can only be recognized on servers that provide this |
|---|
| | 261 | information in the DIR output. |
|---|
| | 262 | |
|---|
| | 263 | - Items that can't be determined at all, are set to None. |
|---|
| | 264 | |
|---|
| | 265 | Currently, ftputil recognizes the MS Robin FTP server. Otherwise, |
|---|
| | 266 | a format commonly used by Unix servers is assumed. If you need to |
|---|
| | 267 | parse output from another server type, please contact me under the |
|---|
| | 268 | email address at the end of this text. |
|---|
| | 269 | |
|---|
| | 270 | stat(path) |
|---|
| | 271 | return stat information also for files which are pointed to by a |
|---|
| | 272 | link. This method follows multiple links until a regular file or |
|---|
| | 273 | directory is found. If an infinite link chain is encountered, a |
|---|
| | 274 | PermanentError is raised. |
|---|
| | 275 | |
|---|
| | 276 | FTPHost.path methods |
|---|
| | 277 | -------------------- |
|---|
| | 278 | |
|---|
| | 279 | FTPHost objects contain an attribute named path, similar to os.path. |
|---|
| | 280 | (See there for documentation.) The following methods can be applied |
|---|
| | 281 | to the remote host with the same semantics as for os.path: abspath, |
|---|
| | 282 | basename, commonprefix, dirname, exists, getmtime, getsize, isabs, |
|---|
| | 283 | isdir, isfile, islink, join, normcase, normpath, split, splitdrive, |
|---|
| | 284 | splitext, and even walk. See the section on the os.path module in the |
|---|
| | 285 | Library Reference, |
|---|
| | 286 | http://www.python.org/doc/current/lib/module-os.path.html |
|---|