Persistence

続けることに意味がある。

WEBrickでWebServerを立てる①

Rubyに組み込まれている簡易WevServerのWEBrickを使ってみる。

ソース

#!C:\Ruby193\bin\evn ruby
require "webrick"
include WEBrick

s = HTTPServer.new(
  :Port => 8000,
  :DocumentRoot => File.join(Dir::pwd,"public_html")
)
trap("INT"){s.shutdown}
s.start    

 

実行エラー①

>ruby httpserver1.rb
httpserver1.rb:8:in `<main>': uninitialized constant Dir::Pwd (NameError)

→Dir::pwd の記述誤り(小文字が正しい)

 

実行エラー②

>ruby httpserver1.rb
[2012-05-11 12:57:30] INFO  WEBrick 1.3.1
[2012-05-11 12:57:30] INFO  ruby 1.9.3 (2012-04-20) [i386-mingw32]
[2012-05-11 12:57:30] WARN  TCPServer Error: Only one usage of each socket addre
ss (protocol/network address/port) is normally permitted. - bind(2)
C:/Ruby193/lib/ruby/1.9.1/webrick/utils.rb:85:in `initialize': Only one usage of
 each socket address (protocol/network address/port) is normally permitted. - bi
nd(2) (Errno::EADDRINUSE)
        from C:/Ruby193/lib/ruby/1.9.1/webrick/utils.rb:85:in `new'
        from C:/Ruby193/lib/ruby/1.9.1/webrick/utils.rb:85:in `block in create_l
isteners'
        from C:/Ruby193/lib/ruby/1.9.1/webrick/utils.rb:82:in `each'
        from C:/Ruby193/lib/ruby/1.9.1/webrick/utils.rb:82:in `create_listeners'

        from C:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:82:in `listen'
        from C:/Ruby193/lib/ruby/1.9.1/webrick/server.rb:70:in `initialize'
        from C:/Ruby193/lib/ruby/1.9.1/webrick/httpserver.rb:45:in `initialize'
        from httpserver1.rb:6:in `new'
        from httpserver1.rb:6:in `<main>'

→ポート番号がすでにほかのサービスで使われていて被ってたっぽい。

 

参考:第1回 WEBrickを使ってみよう!
広告を非表示にする