Do You PHP はてブロ

Do You PHPはてなからはてブロに移動しました

自習メモ〜ネットワークプログラミング

公開されているpdfをやってみるシリーズ(?)。今回はネットワークプログラミングということで、ftpモジュールを使ったファイルのアップロードです。
なお、確認のために、io:format/3で戻り値を全て表示させています。なお、filelib:is_file/1については、2007-05-14 - ます’s Diary - どうでもいい事100選を参考にさせてもらいました :-)

-module(ftp_upload).
-export([upload/6]).

upload(Host, User, Password, RemoteDir, LocalDir, FileList) ->
    {ok, Ftp} = ftp:open(Host),
    io:format("connecting ~p~n", [ftp:user(Ftp, User, Password)]),
    io:format("lpwd ~p~n", [ftp:lpwd(Ftp)]),
    io:format("pwd ~p~n", [ftp:pwd(Ftp)]),
    io:format("cd ~p~n", [ftp:cd(Ftp, RemoteDir)]),
    io:format("lcd ~p~n", [ftp:lcd(Ftp, LocalDir)]),
    io:format("lpwd ~p~n", [ftp:lpwd(Ftp)]),
    io:format("pwd ~p~n", [ftp:pwd(Ftp)]),
    lists:foreach(
        fun(File) ->
            io:format("uploading ~s(~p) => [~p]~n", [File, filelib:is_file(File), ftp:send(Ftp, File)])
        end,
        FileList),
    ftp:close(Ftp).

実行結果は以下のような感じ。

$ pwd
/home/shimooka/work/erlang
]$ ls
ftp_upload.erl
$ erl
Erlang (BEAM) emulator version 5.5.4 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.5.4  (abort with ^G)
1> c(ftp_upload).
{ok,ftp_upload}
2> ftp_upload:upload("ftp.example.com", "ftpuser", "ftppwd", "./html/", "./", ["ftp_upload.erl"]).

=INFO REPORT==== 6-Jun-2007::10:41:56 ===
The inets application was not started. Has now been started as a temporary application.
connecting ok
lpwd {ok,"/home/shimooka/work/erlang"}
pwd {ok,"/"}
cd ok
lcd ok
lpwd {ok,"/home/shimooka/work/erlang"}
pwd {ok,"/html"}
uploading ftp_upload.erl(true) => [ok]
ok
3>

おお、いい感じいい感じ。
やってることは大したことないんですが、コードやホスト名のtypoとか、どうでも良いところでハマって時間を食ってます。。。もうちょっとしっかりせねば。。。