This module implements an asynchronous FTP client. It allows you to connect to an FTP server and perform operations on it such as for example:
- The upload of new files.
- The removal of existing files.
- Download of files.
- Changing of files' permissions.
- Navigation through the FTP server's directories.
Connecting to an FTP server
In order to begin any sort of transfer of files you must first connect to an FTP server. You can do so with the connect procedure.
import asyncdispatch, asyncftpclient proc main() {.async.} = var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test") await ftp.connect() echo("Connected") waitFor(main())
A new main async procedure must be declared to allow the use of the await keyword. The connection will complete asynchronously and the client will be connected after the await ftp.connect() call.
Uploading a new file
After a connection is made you can use the store procedure to upload a new file to the FTP server. Make sure to check you are in the correct working directory before you do so with the pwd procedure, you can also instead specify an absolute path.
import asyncdispatch, asyncftpclient proc main() {.async.} = var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test") await ftp.connect() let currentDir = await ftp.pwd() assert currentDir == "/home/user/" await ftp.store("file.txt", "file.txt") echo("File finished uploading") waitFor(main())
Checking the progress of a file transfer
The progress of either a file upload or a file download can be checked by specifying a onProgressChanged procedure to the store or retrFile procedures.
import asyncdispatch, asyncftpclient proc onProgressChanged(total, progress: BiggestInt, speed: float): Future[void] = echo("Uploaded ", progress, " of ", total, " bytes") echo("Current speed: ", speed, " kb/s") proc main() {.async.} = var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test") await ftp.connect() await ftp.store("file.txt", "/home/user/file.txt", onProgressChanged) echo("File finished uploading") waitFor(main())
Types
AsyncFtpClientObj = FtpBaseObj[AsyncSocket]
- Source Edit
AsyncFtpClient = ref AsyncFtpClientObj
- Source Edit
ProgressChangedProc = proc (total, progress: BiggestInt; speed: float): Future[void] {...}{. closure, gcsafe.}
- Source Edit
Procs
proc send(ftp: AsyncFtpClient; m: string): Future[TaintedString] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc connect(ftp: AsyncFtpClient): Future[void] {...}{.raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc pwd(ftp: AsyncFtpClient): Future[TaintedString] {...}{.raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc cd(ftp: AsyncFtpClient; dir: string): Future[void] {...}{.raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc cdup(ftp: AsyncFtpClient): Future[void] {...}{.raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc listDirs(ftp: AsyncFtpClient; dir = ""): Future[seq[string]] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc existsFile(ftp: AsyncFtpClient; file: string): Future[bool] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc createDir(ftp: AsyncFtpClient; dir: string; recursive = false): Future[void] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc chmod(ftp: AsyncFtpClient; path: string; permissions: set[FilePermission]): Future[ void] {...}{.raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc list(ftp: AsyncFtpClient; dir = ""): Future[string] {...}{.raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc retrText(ftp: AsyncFtpClient; file: string): Future[string] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc defaultOnProgressChanged(total, progress: BiggestInt; speed: float): Future[void] {...}{. nimcall, gcsafe, procvar, raises: [FutureError, Exception], tags: [RootEffect].}
- Default FTP onProgressChanged handler. Does nothing. Source Edit
proc retrFile(ftp: AsyncFtpClient; file, dest: string; onProgressChanged: ProgressChangedProc = defaultOnProgressChanged): Future[ void] {...}{.raises: [FutureError], tags: [RootEffect, TimeEffect, WriteIOEffect].}
- Source Edit
proc store(ftp: AsyncFtpClient; file, dest: string; onProgressChanged: ProgressChangedProc = defaultOnProgressChanged): Future[ void] {...}{.raises: [FutureError], tags: [RootEffect, ReadIOEffect, TimeEffect].}
- Source Edit
proc rename(ftp: AsyncFtpClient; nameFrom: string; nameTo: string): Future[void] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc removeFile(ftp: AsyncFtpClient; filename: string): Future[void] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc removeDir(ftp: AsyncFtpClient; dir: string): Future[void] {...}{. raises: [FutureError], tags: [RootEffect].}
- Source Edit
proc newAsyncFtpClient(address: string; port = Port(21); user, pass = ""): AsyncFtpClient {...}{. raises: [OSError, Exception], tags: [RootEffect].}
- Creates a new AsyncFtpClient object. Source Edit