Why Can't Net::Ftp Connect to Server

Why can't Net::FTP connect to server?

Your code works fine for me. I suspect problem could be because of Net::FTP connection mode, which is by default active. Try connecting using passive mode, following code sample -

ftp = Net::FTP.new(server)
ftp.passive = true
ftp.login user, password
files = ftp.chdir('mydirectory/')
files = ftp.list
puts "list out of directory:"
puts files
ftp.close

And if you're curious, following is the difference (from wikipedia) between active and passive modes.

  1. In Active mode, the client creates a TCP control connection to the server and sends the server the client's IP address and an arbitrary client port number, and then waits until the server initiates the data connection over TCP to that client IP address and client port number. In situations where the client is behind a firewall and unable to accept incoming TCP connections, passive mode may be used.
  2. In Passive mode, the client uses the control connection to send a PASV command to the server and then receives a server IP address and server port number from the server which the client then uses to open a data connection from an arbitrary client port to the server IP address and server port number received.

Ruby Net::FTP - Downloading files from a server

The error message comes from the FTP server. The problem is that the FTP server cannot interpret these unicode filenames. Please check if UTF8 feature is enabled for the connection.

From an existing answer on stackoverflow:

It is not enough for you just to encode your string as UTF8 and send it as filename to FTP server. In the past all FTP servers understood ASCII only and nowadays to maintain backward compatibility - even if they are Unicode aware - when they start they treat all filenemes as ASCII too.

To make it all work you (your program) must first check what your server is capable of. Servers send their features after client connects - in your case you must check for FEAT UTF8. If your server sends that - it means it understands UTF8. Nevertheless - even if it understands it - you must tell it explicitly that from now on you will send your filenames UTF8 encoded and now it is the stuff that your program lacks (as your server supports utf8 as you've stated).

Your client must send to FTP server the following OPTS UTF8 ON. After sending that you may use UTF8 or speak UTF8-ish (so to speak) to your sever.

Read here for details Internationalization of the File Transfer Protocol

Sources:

https://stackoverflow.com/a/19903611/1305200

https://wiki.filezilla-project.org/Character_Encoding

Ruby FTP not working with IIS FTP server

I set passive to false and that solved my problem.



Related Topics



Leave a reply



Submit