FTP: Difference between revisions

1,106 bytes added ,  1 year ago
m
syntax highlighting fixup automation
(Added Bash / Unix Shell Implementation)
m (syntax highlighting fixup automation)
Line 7:
=={{header|BaCon}}==
Using libCURL.
<langsyntaxhighlight lang="bacon">OPTION PARSE FALSE
 
PRAGMA INCLUDE <curl/curl.h>
Line 23:
curl_easy_cleanup(easyhandle)
 
CLOSE FILE download</langsyntaxhighlight>
 
Full native implementation without dependency to external libraries.
<langsyntaxhighlight lang="bacon">FUNCTION interact$(command$, connection, use_pasv)
 
LOCAL pasv$, data$, response$
Line 71:
PRINT interact$("QUIT", ftp, 0)
 
CLOSE NETWORK ftp</langsyntaxhighlight>
 
=={{header|Batch File}}==
This uses the native FTP.EXE in Windows. I am not sure, but I think FTP.EXE client does not support passive mode.
<langsyntaxhighlight lang="dos">::Playing with FTP
::Batch File Implementation
 
Line 98:
echo.get %download%
echo.disconnect
)|ftp -n</langsyntaxhighlight>
{{Out}}
<pre>\Desktop>RCFTP
Line 130:
=={{header|C}}==
Using [http://nbpfaus.net/~pfau/ftplib/ ftplib]
<syntaxhighlight lang="c">
<lang c>
#include <ftplib.h>
 
Line 148:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
Line 154:
 
Using [http://nbpfaus.net/~pfau/ftplib/ ftplib], [https://github.com/saur0n/libftpxx libftp++]
<langsyntaxhighlight lang="cpp"> /* client.cpp
libftp++ C++ classes for ftplib C ftp library
Line 493:
}
/* END */
</syntaxhighlight>
</lang>
 
{{Out}}
Line 539:
Using package [http://code.kepibu.org/cl-ftp/ cl-ftp].
 
<langsyntaxhighlight lang="lisp">(use-package :ftp)
 
(with-ftp-connection (conn :hostname "ftp.hq.nasa.gov"
Line 547:
(let ((filename "Gravity in the Brain.mp3"))
(retrieve-file conn filename filename :type :binary)))
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 565:
=={{header|Erlang}}==
Erlang implementation using ftp module.
<langsyntaxhighlight lang="erlang">
%%%-------------------------------------------------------------------
%%% To execute in shell, Run the following commands:-
Line 608:
io:format("Closing connection to FTP Server"),
ftp:close(Pid).
</syntaxhighlight>
</lang>
 
 
Line 614:
=={{header|FutureBasic}}==
FB for Mac easily interfaces with the terminal command line. NOTE: The curl command line tool used in this example offers upload and sending capabilities. It supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, POP3, IMAP, SMTP, RTMP and RTSP.
<langsyntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
Line 646:
 
HandleEvents
</syntaxhighlight>
</lang>
{{output}}
<pre style="font-size: 12px">
Line 676:
=={{header|Go}}==
Using the FTP package from [https://godoc.org/github.com/stacktic/ftp github.com/stacktic/ftp].
<langsyntaxhighlight lang="go">package main
 
import (
Line 737:
 
fmt.Println("Wrote", n, "bytes to", file)
}</langsyntaxhighlight>
 
 
Line 746:
Dependencies are automatically loaded with the @Grab annotation.
let's say the code is saved in the file ftpTest.groovy:
<syntaxhighlight lang="groovy">
<lang Groovy>
@Grab(group='commons-net', module='commons-net', version='2.0')
import org.apache.commons.net.ftp.FTPClient
Line 761:
}
println(" ...Done.");
</syntaxhighlight>
</lang>
By typing groovy ftpTest.groovy, you should see a README.html file on your directory.
<pre>
Line 782:
Example uses [https://hackage.haskell.org/package/ftphs <tt>ftphs</tt>] package:
 
<langsyntaxhighlight lang="haskell">module Main (main) where
 
import Control.Exception (bracket)
Line 812:
-- Download in binary mode
(fileData, _) <- getbinary h "linux-0.01.tar.gz.sign"
print fileData</langsyntaxhighlight>
 
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j"> require 'web/gethttp'
gethttp 'ftp://anonymous:example@ftp.hq.nasa.gov/pub/issoutreach/Living%20in%20Space%20Stories%20(MP3%20Files)/'
-rw-rw-r-- 1 109 space-station 2327118 May 9 2005 09sept_spacepropulsion.mp3
Line 830:
-rw-rw-r-- 1 109 space-station 1134654 May 9 2005 When Space Makes you Dizzy.mp3
#file=: gethttp rplc&(' ';'%20') 'ftp://anonymous:example@ftp.hq.nasa.gov/pub/issoutreach/Living in Space Stories (MP3 Files)/We have a solution.mp3'
772075</langsyntaxhighlight>
 
=={{header|Java}}==
requires apache.commons.net
<langsyntaxhighlight lang="java">import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Line 906:
}
}
}</langsyntaxhighlight>
 
Output:
Line 954:
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">using FTPClient
 
ftp = FTP(hostname = "ftp.ed.ac.uk", username = "anonymous")
Line 961:
bytes = read(download(ftp, "make.notes.tar"))
 
close(ftp)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
Line 996:
</pre>
Next, you need to compile the following Kotlin program, linking against ftplib.klib.
<langsyntaxhighlight lang="scala">// Kotlin Native v0.6
 
import kotlinx.cinterop.*
Line 1,013:
FtpQuit(vnbuf)
nativeHeap.free(nbuf)
}</langsyntaxhighlight>
Finally, the resulting .kexe file should be executed producing something similar to the following output:
<pre>
Line 1,034:
=={{header|Lingo}}==
{{libheader|Curl Xtra}}
<langsyntaxhighlight lang="lingo">CURLOPT_URL = 10002
ch = xtra("Curl").new()
url = "ftp://domain.com"
Line 1,051:
ch.setOption(CURLOPT_URL, url & filename)
ch.setDestinationFile(_movie.path & filename)
res = ch.exec()</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">libURLSetFTPMode "passive" --default is passive anyway
put url "ftp://ftp.hq.nasa.gov/" into listing
repeat for each line ftpln in listing
Line 1,092:
e.g. to know the working directory, issue "pwd", we could issue "list" for above too,
but using an url with slash on the end with the ftp protocol causes a dir listing by default.
put libURLftpCommand("PWD",ftp.example.org)</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 1,098:
{{works with|Nim|1.4}}
 
<langsyntaxhighlight lang="nim">import asyncdispatch, asyncftpclient
 
const
Line 1,127:
echo await ftp.send("QUIT") # Disconnect.
 
waitFor main()</langsyntaxhighlight>
 
{{out}}
Line 1,149:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use Net::FTP;
 
# set server and credentials
Line 1,170:
$f->type('binary');
$local = $f->get('512KB.zip');
print "Your file was stored as $local in the current directory\n";</langsyntaxhighlight>
{{out}}
<pre>Currently 20 files in the 'upload' directory
Line 1,177:
=={{header|Phix}}==
{{libheader|Phix/libcurl}}
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- libcurl, allocate, file i/o</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">libcurl</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,202:
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"error"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,227:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
$server = "speedtest.tele2.net";
$user = "anonymous";
Line 1,248:
} else {
echo "failed to download file";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,279:
=={{header|PicoLisp}}==
Passive is the default behavior of 'curl'
<langsyntaxhighlight PicoLisplang="picolisp">(in '(curl "-sl" "ftp://kernel.org/pub/site/")
(while (line)
(prinl @) ) )
(call "curl" "-s" "-o" "sha256sums.asc" "ftp://kernel.org/pub/site/sha256sums.asc")</langsyntaxhighlight>
Output:
<pre>README
Line 1,290:
=={{header|Python}}==
{{works with|Python|2.7.10}}
<syntaxhighlight lang="python">
<lang Python>
from ftplib import FTP
ftp = FTP('kernel.org')
Line 1,299:
print ftp.retrbinary('RETR README', open('README', 'wb').write)
ftp.quit()
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Note: <tt>net/ftp</tt> in Racket uses passive mode exclusively.
<langsyntaxhighlight lang="racket">
#lang racket
(require net/ftp)
Line 1,319:
(ftp-download-file conn "." "README")
(ftp-close-connection conn))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 1,325:
{{works with|rakudo|2018.04}}
 
<syntaxhighlight lang="raku" perl6line>use Net::FTP;
 
my $host = 'speedtest.tele2.net';
Line 1,341:
say $_<name> for $ftp.ls;
 
$ftp.get( '1KB.zip', :binary );</langsyntaxhighlight>
 
{{out}}
Line 1,364:
 
=={{header|REBOL}}==
<syntaxhighlight lang="rebol">
<lang REBOL>
system/schemes/ftp/passive: on
print read ftp://kernel.org/pub/linux/kernel/
write/binary %README read/binary ftp://kernel.org/pub/linux/kernel/README
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'net/ftp'
 
Net::FTP.open('ftp.ed.ac.uk', "anonymous","aaa@gmail.com" ) do |ftp|
Line 1,378:
puts ftp.list
ftp.getbinaryfile("make.notes.tar")
end</langsyntaxhighlight>
The connection is closed automatically at the end of the block.
 
=={{header|Rust}}==
Using crate <code>ftp</code> version 3.0.1
<langsyntaxhighlight Rustlang="rust">use std::{error::Error, fs::File, io::copy};
use ftp::FtpStream;
 
Line 1,397:
copy(&mut stream, &mut file)?;
Ok(())
}</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|commons-net}}
<langsyntaxhighlight Scalalang="scala">import java.io.{File, FileOutputStream, InputStream}
 
import org.apache.commons.net.ftp.{FTPClient, FTPFile, FTPReply}
Line 1,478:
ftpClient.logout
}.isFailure) println(s"Failure.")
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scastie.scala-lang.org/3Lq8ehzIQTCuAOPXWofNLw Scastie (JVM)].
 
Line 1,485:
[http://seed7.sourceforge.net/libraries/ftp.htm#openFtp(in_string) open] and handle an
[http://seed7.sourceforge.net/libraries/ftp.htm#ftpFileSys ftpFileSys].
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "ftp.s7i";
 
Line 1,502:
writeln(getFile(ftp, "README"));
close(ftp);
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">require('Net::FTP');
 
var ftp = %s'Net::FTP'.new('ftp.ed.ac.uk', Passive => 1);
Line 1,514:
ftp.binary; # set binary mode
ftp.get("make.notes.tar");
ftp.quit;</langsyntaxhighlight>
 
=={{header|Tcl}}==
===Using package ftp===
<syntaxhighlight lang="tcl">
<lang Tcl>
package require ftp
 
Line 1,528:
::ftp::Type $conn binary
::ftp::Get $conn README README
</syntaxhighlight>
</lang>
 
===Using a virtual file system===
An alternative approach that uses the package [http://sourceforge.net/projects/tclvfs/ TclVFS] to access ftp:// paths as a virtual file system.
 
<langsyntaxhighlight lang="tcl">
package require vfs::urltype
vfs::urltype::Mount ftp
Line 1,546:
}
file copy README [file join $dir README]
</syntaxhighlight>
</lang>
 
The file <tt>vfsftpfix.tcl</tt> with the passive mode patch (see http://wiki.tcl.tk/12837):
<langsyntaxhighlight lang="tcl">
# Replace vfs::ftp::Mount to enable vfs::ftp to work in passive
# mode and make that the default.
Line 1,600:
return $fd
}
</syntaxhighlight>
</lang>
 
=={{header|UNIX Shell}}==
Uses sftp which os available on all Linux distress by default. The commands are identical to ftp. This example uses the public free sftp server at test.rebex.net , the credentials are demo/password :
 
<langsyntaxhighlight lang="bash">
Aamrun $ sftp demo@test.rebex.net
Password:
Line 1,623:
sftp> exit
Aamrun$
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
Line 1,629:
{{libheader|ftplib}}
An embedded program so we can ask the C host to communicate with ''ftplib'' for us.
<langsyntaxhighlight lang="ecmascript">/* ftp.wren */
 
var FTPLIB_CONNMODE = 1
Line 1,660:
ftp.dir("", ".")
ftp.get("ftp.README", "README", FTPLIB_ASCII)
ftp.quit()</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
Line 1,812:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,835:
=={{header|zkl}}==
Using the cURL library, doing this from the REPL. Moving around in the tree isn't supported.
<langsyntaxhighlight lang="zkl">zkl: var cURL=Import("zklCurl")
zkl: var d=cURL().get("ftp.hq.nasa.gov/pub/issoutreach/Living in Space Stories (MP3 Files)/")
L(Data(2,567),1630,23) // downloaded listing, 1630 bytes of header, 23 bytes of trailer
Line 1,846:
L(Data(1,136,358),1681,23)
zkl: File("foo.mp3","w").write(d[0][1681,-23])
1134654 // note that this matches size in listing</langsyntaxhighlight>
The resulting file foo.mp3 has a nice six minute description of what can happen when returning from space.
 
10,333

edits