My simple downloader doesn't work

Hello,

I’m learning Ruby for some time. Today I started to write simple code
which will download specified files from specified servers but it
doesn’t work. It always showing me error 500 when code is going to
download file. This is code:

require ‘net/ftp’
require ‘net/http’

class Get

def initialize(resource)
if resource=~/http://|ftp:///
@resource=resource
@status=0
@type = resource[0…1].to_s
end
end

def get
case (@type)
when “ht”
@sock = Net.HTTP.new(@resource)
@sock.getbinaryfile(“index.html”,1024)
when “ft”
index = (@resource[6…-1].index("/")+5)
last = (@resource.size - @resource.reverse.index("/")-1)
ftp = Net::FTP.new(@resource[6…index])
ftp.login
ftp.chdir(@resource[(index+2)…last])
ftp.get(@resource[(last+1)…-1] , @resource[(last+1)…-1])
ftp.close
end
end

end
get = Get.new(“ftp://sunsite.icm.edu.pl/pub/Linux/slackware/
slackware-12.0/CHECKSUMS.md5.asc”)
get.get

On Wed, Jul 30, 2008 at 1:30 PM, Mateusz T. [email protected]
wrote:

Hello,

I’m learning Ruby for some time. Today I started to write simple code
which will download specified files from specified servers but it
doesn’t work. It always showing me error 500 when code is going to
download file. This is code:

I didn’t find out why your code doesn’t work, but using open-uri from
the stdlib works, and might be nicer:

require “open-uri”
=> true

puts open(“ftp://anonymous:[email protected]/pub/Linux/slackware/slackware-12.0/CHECKSUMS.md5.asc”).read
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkiPY8sACgkQakRjwEAQIjOctwCggx3kItwUmkXNFmhUko2M9jxd
2v8AnjfTZFeZbJlQZ3Pb8UTtkbq3df8m
=lKsD
-----END PGP SIGNATURE-----

It still don’t work:

require ‘open-uri’
puts open(“anonymous:[email protected]/pub/Linux/slackware/slackware-12.0/CHECKSUMS.md5.asc”).read
Net:FTPPermError: 500 Illegal PORT Command

I thinking about some bug in open-uri and net/ftp. Code is simple and
it runs so that’s not mine error. Of course i succesfully downloaded
this file using Firefox3 and TotalCommander.
Code was tested on winXP and Kubuntu8.04.

Maybe because active ftp and your firewall, try:

require ‘open-uri’
open(“ftp://whatever.ftp.site”, :ftp_active_mode => false).read

or with net/ftp call:
ftp.passive

martin

Martin B. wrote:

Maybe because active ftp and your firewall, try:

require ‘open-uri’
open(“ftp://whatever.ftp.site”, :ftp_active_mode => false).read

or with net/ftp call:
ftp.passive

martin

There’s no working firewall or ftp server on my host.
Your code with using ‘open-uri’ throws error unrecognized option:
ftp_active_mode (Argument Error)

On 1 Aug 2008, at 14:46, Gregory B. wrote:

this file using Firefox3 and TotalCommander.
Code was tested on winXP and Kubuntu8.04.

The code I showed worked on my machine. Tested on OS X (10.4),
without any firewall issues.
Ruby version:

Beyond firewalls, active ftp won’t work behind a NAT device.
Ftp servers sometimes say illegal port command if you tell them that
your address is a private ip address like 192.168.x (your address on
the network behind the nat device)

Fred

On Thu, Jul 31, 2008 at 3:29 AM, WujcioL [email protected] wrote:

It still don’t work:

require ‘open-uri’
puts open(“anonymous:[email protected]/pub/Linux/slackware/slackware-12.0/CHECKSUMS.md5.asc”).read
Net:FTPPermError: 500 Illegal PORT Command

I thinking about some bug in open-uri and net/ftp. Code is simple and
it runs so that’s not mine error. Of course i succesfully downloaded
this file using Firefox3 and TotalCommander.
Code was tested on winXP and Kubuntu8.04.

The code I showed worked on my machine. Tested on OS X (10.4),
without any firewall issues.
Ruby version:

seltzer:~ sandal$ ruby -v
ruby 1.8.6 (2008-06-20 patchlevel 230) [i686-darwin8.11.1]

On 1 Aug 2008, at 15:20, Mateusz T. wrote:

Beyond firewalls, active ftp won’t work behind a NAT device.
Ftp servers sometimes say illegal port command if you tell them that
your address is a private ip address like 192.168.x (your address on
the network behind the nat device)

Fred

It can be it cause i’m connecting from small LAN. Got any idea how to
make it works even that host is beheind nat device?

You need to be using passive ftp. According to the docs you just need
to say ftp.passive = true (assuming ftp is an instance of Net::FTP)

Fred

Frederick C. wrote:

On 1 Aug 2008, at 14:46, Gregory B. wrote:

this file using Firefox3 and TotalCommander.
Code was tested on winXP and Kubuntu8.04.

The code I showed worked on my machine. Tested on OS X (10.4),
without any firewall issues.
Ruby version:

Beyond firewalls, active ftp won’t work behind a NAT device.
Ftp servers sometimes say illegal port command if you tell them that
your address is a private ip address like 192.168.x (your address on
the network behind the nat device)

Fred

It can be it cause i’m connecting from small LAN. Got any idea how to
make it works even that host is beheind nat device?

Frederick C. wrote:

On 1 Aug 2008, at 15:20, Mateusz T. wrote:

Beyond firewalls, active ftp won’t work behind a NAT device.
Ftp servers sometimes say illegal port command if you tell them that
your address is a private ip address like 192.168.x (your address on
the network behind the nat device)

Fred

It can be it cause i’m connecting from small LAN. Got any idea how to
make it works even that host is beheind nat device?

You need to be using passive ftp. According to the docs you just need
to say ftp.passive = true (assuming ftp is an instance of Net::FTP)

Fred

With ftp.passive my first code works but version using open-uri was
smaller. So I’m asking about how to set passive mode using open-uri?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs