On this page:
4.1 Functions
ftp-connection?
ftp-establish-connection
ftp-close-connection
ftp-cd
ftp-directory-list
ftp-make-file-seconds
ftp-download-file
ftp-upload-file
ftp-delete-file
ftp-make-directory
ftp-delete-directory
ftp-rename-file
4.2 FTP Unit
ftp@
4.3 FTP Signature
ftp^

4 FTP: Client

Micah Flatt
and Chen Xiao <chenxiao770117@gmail.com>

 (require net/ftp) package: net-lib
The net/ftp library provides utilities for FTP client operations.

4.1 Functions

procedure

(ftp-connection? v)  boolean?

  v : any/c
Returns #t if v represents an FTP connection as returned by ftp-establish-connection, #f otherwise.

procedure

(ftp-establish-connection server    
  port-no    
  user    
  passwd)  ftp-connection?
  server : string?
  port-no : (integer-in 0 65535)
  user : string?
  passwd : string?
Establishes an FTP connection with the given server using the supplied username and password. The port-np argument usually should be 21.

procedure

(ftp-close-connection ftp-conn)  void?

  ftp-conn : ftp-connection?
Closes an FTP connection.

procedure

(ftp-cd ftp-conn new-dir)  void?

  ftp-conn : ftp-connection?
  new-dir : string?
Changes the current directory on the FTP server to new-dir. The new-dir argument is not interpreted at all, but simply passed on to the server; it must not contain a newline.

procedure

(ftp-directory-list ftp-conn [path])

  
(listof (list/c (one-of/c "-" "d" "l")
                string?
                string?))
  ftp-conn : ftp-connection?
  path : (or/c false/c string?) = #f
Returns a list of files and directories in the current directory of the server, assuming that the server provides directory information in the quasi-standard Unix format. If a path argument is given, use it instead of the current directory.

Each file or directory is represented by a list of three or four strings. The first string is either "-", "d", or "l", depending on whether the items is a file, directory, or link, respectively. The second item is the file’s date; to convert this value to seconds consistent with file-seconds, pass the date string to ftp-make-file-seconds. The third string is the name of the file or directory. If the item is a file (the first string is "-"), and if the line that the server replied with has a size in the expected place, then a fourth string containing this size is included.

Warning: the FTP protocol has no specification for the reply format, so this information can be unreliable.

procedure

(ftp-make-file-seconds ftp-date)  exact-integer?

  ftp-date : string?
Takes a date string produced by ftp-directory-list and converts it to seconds (which can be used with seconds->date).

Warning: the FTP protocol has no specification for the reply format, so this information can be unreliable.

procedure

(ftp-download-file ftp-conn    
  local-dir-or-port    
  file    
  [#:progress progress-proc])  void?
  ftp-conn : ftp-connection?
  local-dir-or-port : 
(or/c path-string?
      output-port?)
  file : string?
  progress-proc : 
(or/c #f
      (-> (-> (values exact-nonnegative-integer?
                      evt?))
          any))
   = #f
If local-dir-or-port is a path-string?, ftp-download-file downloads file from the server’s current directory and puts it in local-dir-or-port using the same name. If the file already exists in the local directory, it is replaced, but only after the transfer succeeds (i.e., the file is first downloaded to a temporary file in local-dir-or-port, then moved into place on success).

When local-dir-or-port is an output-port?, it downloads file from the server’s current directory and writes its content to provided output-port?. The data is written to the port as it is being received from the server and if the download is interrupted, incomplete data is written to the port. The port is closed after finishing the transfer.

If progress-proc is not #f, then it is called with a function get-count that returns two values: the number of bytes transferred so far, and an event that becomes ready when the transferred-bye count changes. The get-count function can be called in any thread and any number of times. The progress-proc function should return immediately, perhaps starting a thread that periodically polls get-count. Do not poll too frequently, or else polling will slow the transfer; the second argument from get-count is intended to limit polling.

(ftp-download-file
 ftp-conn "." "testfile"
 #:progress
 (lambda (get-count)
   (thread
    (lambda ()
      (let loop ()
        (define-values (count changed-evt) (get-count))
        (printf "~a bytes downloaded\n" count)
        (sync changed-evt)
        (loop))))))

procedure

(ftp-upload-file ftp-conn    
  file-path    
  [port    
  #:progress progress-proc])  void?
  ftp-conn : ftp-connection?
  file-path : path-string?
  port : (or/c #f input-port?) = #f
  progress-proc : 
(or/c #f
      (-> (-> (values exact-nonnegative-integer?
                      evt?))
          any))
   = #f
When port is #f, upload file-path to the server’s current directory using the same name. If the file already exists in the remote directory, it is replaced. The progress-proc argument is used in the same way as in ftp-download-file, but to report uploaded bytes instead of downloaded bytes.

If port is an input-port?, the content of that port is streamed as upload to the server and stored under given file-path name. The port is closed after finishing the transfer.

procedure

(ftp-delete-file ftp-conn file-path)  void?

  ftp-conn : ftp-connection?
  file-path : path-string?
Delete the remote file use the file-path on the server.

procedure

(ftp-make-directory ftp-conn dir-name)  void?

  ftp-conn : ftp-connection?
  dir-name : string?
Make remote directory use the dir-name.

procedure

(ftp-delete-directory ftp-conn dir-name)  void?

  ftp-conn : ftp-connection?
  dir-name : string?
Delete remote directory use the dir-name.

procedure

(ftp-rename-file ftp-conn origin dest)  void?

  ftp-conn : ftp-connection?
  origin : string?
  dest : string?
Rename remote file name from origin to dest.

4.2 FTP Unit

ftp@ and ftp^ are deprecated. They exist for backward-compatibility and will likely be removed in the future. New code should use the net/ftp module.

 (require net/ftp-unit) package: compatibility-lib

value

ftp@ : unit?

Imports nothing, exports ftp^.

4.3 FTP Signature

 (require net/ftp-sig) package: compatibility-lib

signature

ftp^ : signature

Includes everything exported by the net/ftp module.