W3cubDocs

/Nim

Module ftpclient

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.

Imports

sockets, strutils, parseutils, times, os, asyncio, asyncnet, nativesockets, asyncdispatch

Types

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
Async alternative to TFTPClient.
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.
Event
ReplyError = object of IOError
FTPError = object of IOError

Procs

proc ftpClient(address: string; port = Port(21); user, pass = ""): FtpClient {.
    raises: [OSError], tags: [].}
Create a 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])
Connect to the FTP server specified by ftp.
proc pwd[T](ftp: FtpBase[T]): string
Returns the current working directory.
proc cd[T](ftp: FtpBase[T]; dir: string)
Changes the current directory on the remote FTP server to dir.
proc cdup[T](ftp: FtpBase[T])
Changes the current directory to the parent of the current directory.
proc listDirs[T](ftp: FtpBase[T]; dir: string = ""; async = false): seq[string]
Returns a list of filenames in the given directory. If 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)
Creates a directory 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])
Changes permission of path to permissions.
proc list[T](ftp: FtpBase[T]; dir: string = ""; async = false): string
Lists all files in 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
Retrieves 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)
Downloads 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)
Uploads 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])
Terminates the connection to the server.
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: [].}
Registers ftp with dispatcher d.

© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/ftpclient.html