"Rubyによるクローラー開発技法"という本で紹介されていた方法でテストサーバーを立てようとしているのですが、うまくいきません。
その方法とは、test-webserver0.rbというファイルを作成し、terminalから
terminal
1ruby test-webserver0.rb
と呼び出すものです。
以下がtest-webserver0.rbです。
Ruby
1#!/usr/local/bin/ruby 2# -*- coding: utf-8 -*- 3require 'webrick' 4 5class TestContentServlet < WEBrick::HTTPServlet::AbstractServlet 6 7def do_GET(req, res) 8 #拡張子で分岐 9 res.body = case req.path 10 when /¥.html$/; html_content req.path 11 when /¥.txt$/; txt_content req.path 12 else; "dummy" 13 end 14 res.content_type = WEBrick::HTTPUtils.mime_type( 15 req.path_info, WEBrick::HTTPUtils::DefaultMimeTypes) 16 end 17 18 def html_content(path) 19 node = path[0..-6] 20 <<HTML 21<html><head><title>#{path}</title><head> 22<body><p> 23<a href="#{node}/1.html">#{node}/1.html</a><br> 24<a href="#{node}/2.html">#{node}/2.html</a><br> 25<a href="#{node}.txt">#{node}.txt</a><br> 26<a href="http://localhost:7777#{node}.org">#{node}.org</a><br> 27<a href="/1.html">/1.html</a> 28</p></body></html> 29HTML 30 end 31 32 def txt_content(path) 33 "This is #{path}" 34 end 35end 36 37srv = WEBrick::HTTPServer.new( 38 :BindAddress => '127.0.0.1', :Port => 7777) 39srv.mount('/', TestContentServlet) 40trap("INT"){ srv.shutdown } 41srv.start
見ての通り、#{path}や#{node}以降がコメントになりうまく動きません。
#(シャープ)以降がコメントになるのはRubyの特徴なので当然といえば当然なのですが、本にはこの例が載っていたので、よくわかりません。
また、http://127.0.0.1:7777/1.htmlにアクセスしても、常にdummyと表示されてしまいます。
何が間違っているのでしょうか?ご教授宜しくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。