Note: This module is deprecated since version 0.11.3. You should use the async version of this module asyncftpclient.
This module partially implements an FTP client as specified by RFC 959.
This module provides both a synchronous and asynchronous implementation. The asynchronous implementation requires you to use the asyncFTPClient
function. You are then required to register the AsyncFTPClient
with a asyncio dispatcher using the register
function. Take a look at the asyncio module documentation for more information.
Note: The asynchronous implementation is only asynchronous for long file transfers, calls to functions which use the command socket will block.
Here is some example usage of this module:
var ftp = ftpClient("example.org", user = "user", pass = "pass") ftp.connect() ftp.retrFile("file.ext", "file.ext")
Warning: The API of this module is unstable, and therefore is subject to change.
FtpBase[SockType] = ref FtpBaseObj[SockType]
FtpBaseObj[SockType] = object csock*: SockType dsock*: SockType when SockType is AsyncSocket: handleEvent disp asyncDSockID user*, pass*: string address*: string when SockType is AsyncSocket: port else: port jobInProgress*: bool job*: FtpJob[SockType] dsockConnected*: bool
FTPJobType = enum JRetrText, JRetr, JStore
FtpClientObj = FtpBaseObj[Socket]
FtpClient = ref FtpClientObj
AsyncFtpClient = ref AsyncFtpClientObj
AsyncFtpClientObj = FtpBaseObj[AsyncSocket]
FTPEventType = enum EvTransferProgress, EvLines, EvRetr, EvStore
FTPEvent = object filename*: string case typ*: FTPEventType of EvLines: lines*: string ## Lines that have been transferred. of EvRetr, EvStore: ## Retr/Store operation finished. nil of EvTransferProgress: bytesTotal*: BiggestInt ## Bytes total. bytesFinished*: BiggestInt ## Bytes transferred. speed*: BiggestInt ## Speed in bytes/s currentJob*: FTPJobType ## The current job being performed.
ReplyError = object of IOError
FTPError = object of IOError
proc ftpClient(address: string; port = Port(21); user, pass = ""): FtpClient {...}{. raises: [OSError], tags: [].}
FtpClient
object. proc send[T](ftp: FtpBase[T]; m: string): TaintedString
Send a message to the server, and wait for a primary reply. \c\L
is added for you.
Note: The server may return multiple lines of coded replies.
proc connect[T](ftp: FtpBase[T])
ftp
. proc pwd[T](ftp: FtpBase[T]): string
proc cd[T](ftp: FtpBase[T]; dir: string)
dir
. proc cdup[T](ftp: FtpBase[T])
proc listDirs[T](ftp: FtpBase[T]; dir: string = ""; async = false): seq[string]
dir
is "", the current directory is used. If async
is true, this function will return immediately and it will be your job to use asyncio's poll
to progress this operation. proc fileExists(ftp: FtpClient; file: string): bool {...}{.deprecated, raises: [FTPError, Exception, OSError, ValueError, TimeoutError, ReplyError, OverflowError], tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect].}
Deprecated since version 0.9.0: Please use existsFile
.
Determines whether file
exists.
Warning: This function may block. Especially on directories with many files, because a full list of file names must be retrieved.
proc existsFile(ftp: FtpClient; file: string): bool {...}{.raises: [FTPError, Exception, OSError, ValueError, TimeoutError, ReplyError, OverflowError], tags: [RootEffect, WriteIOEffect, ReadIOEffect, TimeEffect].}
Determines whether file
exists.
Warning: This function may block. Especially on directories with many files, because a full list of file names must be retrieved.
proc createDir[T](ftp: FtpBase[T]; dir: string; recursive: bool = false)
dir
. If recursive
is true, the topmost subdirectory of dir
will be created first, following the secondmost... etc. this allows you to give a full path as the dir
without worrying about subdirectories not existing. proc chmod[T](ftp: FtpBase[T]; path: string; permissions: set[FilePermission])
path
to permissions
. proc list[T](ftp: FtpBase[T]; dir: string = ""; async = false): string
dir
. If dir
is ""
, uses the current working directory. If async
is true, this function will return immediately and it will be your job to call asyncio's poll
to progress this operation. proc retrText[T](ftp: FtpBase[T]; file: string; async = false): string
file
. File must be ASCII text. If async
is true, this function will return immediately and it will be your job to call asyncio's poll
to progress this operation. proc retrFile[T](ftp: FtpBase[T]; file, dest: string; async = false)
file
and saves it to dest
. Usage of this function asynchronously is recommended to view the progress of the download. The EvRetr
event is passed to the specified handleEvent
function when the download is finished, and the filename
field will be equal to file
. proc store[T](ftp: FtpBase[T]; file, dest: string; async = false)
file
to dest
on the remote FTP server. Usage of this function asynchronously is recommended to view the progress of the download. The EvStore
event is passed to the specified handleEvent
function when the upload is finished, and the filename
field will be equal to file
. proc close[T](ftp: FtpBase[T])
proc asyncFTPClient(address: string; port = Port(21); user, pass = ""; handleEvent: proc ( ftp: AsyncFtpClient; ev: FTPEvent) {...}{.closure, gcsafe.} = ( proc (ftp: AsyncFtpClient; ev: FTPEvent) = discard )): AsyncFtpClient {...}{. raises: [OSError], tags: [].}
Create a AsyncFTPClient
object.
Use this if you want to use asyncio's dispatcher.
proc register(d: Dispatcher; ftp: AsyncFtpClient): Delegate {...}{.discardable, raises: [], tags: [].}
ftp
with dispatcher d
.
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/ftpclient.html