how to do an ftp connection properly

276 views
Skip to first unread message

António Ramos

unread,
Dec 15, 2015, 5:21:04 PM12/15/15
to elixir-l...@googlegroups.com
Hello i saw this code to connect to an ftp server.
However for a real code it would have to be changed to trap errors in between right?
I´m newbie and dont know how to fix this issue.
Can someone give me some ideas ?
Regards

:inets.start
{:ok, pid} = :inets.start(:ftpc, host: 'erlang.org')
:ftp.user(pid, 'guest', 'password')
:ftp.pwd(pid)
:ftp.cd(pid, 'appl/examples')
:ftp.lpwd(pid)
:ftp.lcd(pid, '/home/eproj/examples')
:ftp.recv(pid, 'appl.erl')
:inets.stop(:ftpc, pid)

António Ramos

unread,
Dec 15, 2015, 6:28:05 PM12/15/15
to elixir-lang-talk
My newbie way of doing it.
Doing fine until reached chdir on this line of code
IO.inspect :ftp.cd(pid, "/MEMCARD")

i get this error 
     ** (FunctionClauseError) no function clause matching in :ftp.is_name_sane/1
     stacktrace:
       (inets) ftp.erl:2454: :ftp.is_name_sane("/MEMCARD")
       (inets) ftp.erl:283: :ftp.cd/2
       (ps1) lib/ps1.ex:24: Ps1.chdir/2
       test/ps1_test.exs:6

defmodule Ps1 do
@ps1 'localhost'
@user 'cires'
@pwd  'cires'

def checkftp() do
IO.puts "checkftp"
:ok=:inets.start
end
def initftp(:ok) do
IO.puts "initftp"
{:ok, pid} = :inets.start(:ftpc, host: @ps1)
pid
end
def userftp(pid) do
IO.puts "userftp"
case :ftp.user(pid, @user, @pwd) do
:ok ->pid
_ -> {:error}
end
end
def chdir pid,_folder do 
IO.inspect :ftp.pwd(pid)
IO.inspect :ftp.cd(pid, "/MEMCARD")

# case :ftp.cd(pid, "/MEMCARD") do
# :ok -> pid
# _ -> :error
# end
end 
IO.puts "starting"
def run do
checkftp
|>initftp
|>userftp
# |>IO.inspect
|>chdir "MEMCARD"
end
end

Ben Wilson

unread,
Dec 15, 2015, 7:15:37 PM12/15/15
to elixir-lang-talk
Note the use of single quotes in the example you posted first. They aren't the same as double quotes. see: http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html. Many erlang libraries expect char lists. Check the documentation on the given functions.

Ben Wilson

unread,
Dec 15, 2015, 8:29:21 PM12/15/15
to elixir-lang-talk
Oh also it's very important to note that "" in erlang are equivalent to '' in elixir. Thus if you see "foo" in erlang you want 'foo' in elixir. Erlang strings are elixir chat lists.
Reply all
Reply to author
Forward
0 new messages