There are at least two HTTP client libraries for Erlang in addition to httpc
in the OTP library (which seems to be generally regarded as buggy and clunky): ibrowse
and lhttpc
. Are there similar alternatives to ftp
?
I realize this is an old question, but hopefully others will find this useful:
lftpc is a "Lightweight Erlang FTP Client" modeled after lhttpc and dlhttpc which we have been using in production for the last 6 months or so.
It's not very well documented right now, but here is an example using test.rebex.net
:
1> lftpc:start().
ok
2> {ok, {_, _, Socket}} = lftpc:connect("test.rebex.net", 21, []).
{ok,{undefined,{220,[<<"FTP on test.rebex.net ready...">>]},
<0.65.0>}}
3> lftpc:login(Socket, [{username, <<"demo">>}, {password, <<"password">>}], infinity, []).
{ok,[{undefined,{331,[<<"Password required for demo.">>]},
undefined},
{undefined,{230,[<<"User demo logged in.">>]},undefined}]}
4> lftpc:cd(Socket, <<"pub">>, infinity, []).
{ok,{undefined,{250,
[<<"CWD command successful. \"/pub\" is current directory.">>]},
undefined}}
By default, there are no decoders used for the control or data connections. So when we list a directory containing 2 files: example
and test
we get back:
5> lftpc:nlist(Socket, infinity, []).
{ok,{{150,
[<<"Data connection accepted from 173.198.175.141:53504; transfer starting.">>]},
{226,[<<"Transfer ok.">>]},
[<<"example\r\ntest\r\n">>]}}
We can specify a data_decoder
:
6> lftpc:nlist(Socket, infinity, [{partial_download, []}, {data_decoder, lftpc_format:nlst_decoder()}]).
{ok,{{150,
[<<"Data connection accepted from 127.0.0.1:54359; transfer starting.">>]},
{226,[<<"Transfer ok.">>]},
[[<<"example">>,<<"test">>]]}}
We can also specify partial_download
to receive the data in chunks:
7> {ok, {_, Download}} = lftpc:nlist(Socket, infinity, [{partial_download, []}, {data_decoder, lftpc_format:nlst_decoder()}]).
{ok,{{150,
[<<"Data connection accepted from 127.0.0.1:54403; transfer starting.">>]},
<0.86.0>}}
8> receive {data_part, Download, Data} -> Data end.
[<<"example">>,<<"test">>]
9> receive {ftp_eod, Download, Message} -> Message end.
{226,[<<"Transfer ok.">>]}
Take a look at the main src/lftpc.erl
file, there are high-level and low-level functions depending on your needs. You can always drop down to lftpc:request/3,4,5,6
and lftpc:start_transfer/3
to do anything custom.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With