I'm new to ruby, and I've run into a problem while reading 'Programming Ruby'. The 'Basic Input and Output' chapter has this example (I've added the 'puts' statements):
require 'net/http'
h = Net::HTTP.new('www.pragmaticprogrammer.com', 80) puts 'before get' resp, data = h.get('/index.html', nil) puts 'after get' if resp.message == "OK" data.scan(/<img src="(.*?)"/) { |x| puts x } end
The above code hangs (but terminates ok with ctl-break) after 'before get', and never reaches 'after get'. There is something up with my machine, because a coworker can run this fine on his.
Things I've tried :
1. 'ping www.pragmaticprogrammer.com <http://www.pragmaticprogrammer.com/> ' from the same command prompt. This works fine. 2. try with other web sites. No difference 3. uninstalled, registry cleanup (CCleaner), reboot and reinstalled ruby versions 182-14 and 182-15. No difference. 4. 'gem list ---remote' output gets to 'Updating Gem source index for::/gems.rubyforge.org' and then hangs just like the above code. 5. Turned off firewall (ZoneAlarm) and anti-spyware (Microsoft). No difference. 6. Added exception handling. No difference.
> 5. Turned off firewall (ZoneAlarm) - No difference.
I would have suspected that, first. You *closed* ZA ? (Not recommended ever, while online, BTW)
You should have an entry under programs for: Ruby interpreter 1,8,2,0 For this script you need: Allow connect (Internet) I have "?Ask" Server (Local/Internet)
OK, you say it's not a firewall problem.
> Any suggestions?
> ruby 1.8.2 (2004-12-25) [i386-mswin32]
Here's a trace log of what should happen on your version of Ruby.
Try running this modified version of your script and report which trace line number you reach. If the last 10-20 lines of your trace vary from mine, post them here and someone who understands this stuff may be able to help.
I have not been following this thread closely so excuse me if I repeat advice from others. Since you are running windows and it looks like your are trying to access a raw socket maybe you have some kind of security setting that prevents raw socket access? I seem to recall some security update/recommended patch/tweak that prevented raw socket access from arbitrary programs.
(It's surprising that it doesn't time out, though.)
All I can suggest is that you try with a local server. If you set this going in the background, you should get a response from your browser at http://127.0.0.1/webrick :
<servlet.rb> #------------------------------------------------- require 'webrick' include WEBrick
class HelloServlet < HTTPServlet::AbstractServlet def do_GET(req, res) # show request in response ! res.body = req.to_s res['Content-Type'] = "text/plain" end end s.mount('/webrick', HelloServlet)
trap("INT") { s.shutdown } STDOUT.sync=true puts ' '*30 << '**********************************' puts ' '*30 << '== Ctrl+Break to close server ==' puts ' '*30 << '**********************************' s.start #-------------------------------------------------
Then, if everything is OK of course, you could access it from another Ruby session with your modified script:
h = Net::HTTP.new('127.0.0.1', 80) h.set_debug_output STDOUT resp, data = h.get('/webrick/xyz', nil)
puts '*'*30 if resp.message =~ /\AOK/ print data else puts 'ERROR ...' p resp.message end puts '*'*30 #-------------------------------------------------
It does seem like a problem you've got locally rather than Ruby's Net library. Please post back if you find the cause or suspect 'Net' is at fault.
I think you guys are having the same problem with windows and zone alarm that some people were having on the rails list. Zone alarm keeps ruby from working with sockets correctly. The only way they fixed this was by uninstalling zone alarm. Just turning it off didn't work but uninstalling it let ruby have its sockets back.
> (It's surprising that it doesn't time out, though.)
> All I can suggest is that you try with a local server. > If you set this going in the background, you should > get a response from your browser at http://127.0.0.1/webrick :
> <servlet.rb> > #------------------------------------------------- > require 'webrick' > include WEBrick
> h = Net::HTTP.new('127.0.0.1', 80) > h.set_debug_output STDOUT > resp, data = h.get('/webrick/xyz', nil)
> puts '*'*30 > if resp.message =~ /\AOK/ > print data > else > puts 'ERROR ...' > p resp.message > end > puts '*'*30 > #-------------------------------------------------
> It does seem like a problem you've got locally rather than > Ruby's Net library. Please post back if you find the cause > or suspect 'Net' is at fault.
Ezra Zygmuntowicz wrote: > I think you guys are having the same problem with windows and zone > alarm that some people were having on the rails list. Zone alarm > keeps ruby from working with sockets correctly. The only way they > fixed this was by uninstalling zone alarm. Just turning it off didn't > work but uninstalling it let ruby have its sockets back.
> -Ezra
Thanks for the tip, Ezra.
This Jeff Nadeau blog entry is very interesting: http://www.jfnadeau.com/node/13 Describes the Ruby under Windows problem !! (Thanks, Jeff)
Ezra Zygmuntowicz wrote: > I think you guys are having the same problem with windows and zone > alarm that some people were having on the rails list. Zone alarm > keeps ruby from working with sockets correctly. The only way they > fixed this was by uninstalling zone alarm. Just turning it off didn't > work but uninstalling it let ruby have its sockets back.
> -Ezra
Bingo. I uninstalled ZoneAlarm and now I can HTTP#put to my heart's content. I can also run gem --remote.
daz wrote: > Which Windows and ZA do you have ?
I've got winxp sp2 with all the latest updates, and ZoneAlarm Suite 5-5-094, with all their latest updates. The other computer that didn't have this problem had the same winxp and ZoneAlarm updates, except that I had the paid version and he had the free version.
I'll be trying it again with the free version, and contacting ZoneAlarm for a workaround. I'll post again to this thread if I find one.