My simple downloader doesn't work

M

Mateusz Tybura

[Note: parts of this message were removed to make it a legal post.]

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
 
G

Gregory Brown

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:
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

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

WujcioL

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.
 
M

Martin Boese

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
 
M

Mateusz Tybura

Martin said:
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)
 
G

Gregory Brown

It still don't work:

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]
 
F

Frederick Cheung

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
seltzer:~ sandal$ ruby -v
ruby 1.8.6 (2008-06-20 patchlevel 230) [i686-darwin8.11.1]
 
M

Mateusz Tybura

Frederick said:
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?
 
F

Frederick Cheung

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
 
M

Mateusz Tybura

Frederick said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top