[ruby-list:50502] Re: [質問] net/http.rb 内 method の一部のraise抑止したい
From:
Masahiro Utsumi <utsumi@...>
Date:
2017-03-14 07:26:54 UTC
List:
ruby-list #50502
内海@ベルギーです
ご自作のスクリプトがわからないのですが、ポートが
開いているかを確認してから呼べばよいのでは?
require 'socket'
def is_open?(port)
begin
socket = TCPSocket.open("localhost", port)
socket.close
return true
rescue Exception => e
puts e.to_s
return false
end
end
的外れだったらすみません。
On 2017/03/14 7:03, yamataka@u08.itscom.net wrote:
> 山口と申します
>
> Windows 8.1 Enterprise
>> ver
> Microsoft Windows [Version 6.3.9600]
>> ruby –version
> Ruby 2.3.1p112 (2016-04-26 revision 54768) [x64-ming32]
>
> の環境で、
>
> 自作のスクリプトが、
> Failed to open TCP connection to 192.168.0.115:35803 (No connection
> could be made because the target machine actively refused it. - connect(
> 2) for "192.168.0.115" port 35803)
> と、net/http.rb 内 connect method 内 raise により、スクリプトが止まるの
> で、
>
> C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb:882: `No connection could be
> made because the target machine actively refused it. - connect(2) for "
> 192.168.0.115" port 35803' (Errno::ECONNREFUSED)
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:319:in `open_http'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_
> open'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:212:in `block in
> open_loop'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:717:in `open'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:35:in `open'
> from c:/yama/bin/UPnP/EasyUpnpWrap.rb:24:in `fetch_device_name'
> from c:/yama/bin/UPnP/EasyUpnpWrap.rb:15:in `device_name'
> from ./au_easy_upnp.rb:71:in `block in <main>'
> from ./au_easy_upnp.rb:70:in `each'
> from ./au_easy_upnp.rb:70:in `<main>'
> C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb:882: raise e, "Failed
> to open TCP connection to " +
>
> C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb の 882行目辺りの raise している
> 箇所を、下記の様に直接編集
>
> def connect
> …
> =begin # original begin
> raise e, "Failed to open TCP connection to " +
> "#{conn_address}:#{conn_port} (#{e.message})"
> =end # original end
> # modified begin
> puts "Failed to open TCP connection to " +
> "#{conn_address}:#{conn_port} (#{e.message})"
> return nil # 何も考えず nil で返しました。
> # modified end
> …
>
> 再度スクリプト実行すると、
>
> C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb:1476: `undefined method `closed?
> ' for nil:NilClass
> Did you mean? clone' (NoMethodError)
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_
> open'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:212:in `block in
> open_loop'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:717:in `open'
> from C:/Ruby23-x64/lib/ruby/2.3.0/open-uri.rb:35:in `open'
> from c:/yama/bin/UPnP/EasyUpnpWrap.rb:24:in `fetch_device_name'
> from c:/yama/bin/UPnP/EasyUpnpWrap.rb:15:in `device_name'
> from ./au_easy_upnp.rb:71:in `block in <main>'
> from ./au_easy_upnp.rb:70:in `each'
> from ./au_easy_upnp.rb:70:in `<main>'
> C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb:1476: raise exception
>
> と、またraise によりスクリプトが停止するので、下記の様に
>
> def transport_request(req)
> …
> rescue => exception
> D "Conn close because of error #{exception}"
> @socket.close if @socket and not @socket.closed?
> =begin # original
> raise exception
> =end
> end
>
> 直接編集で、とりあえず回避できるようになったので、上の暫定変更を元に戻し、
>
> httpWrap.rb を下記の様に作成
> C:/Ruby23-x64/lib/ruby/2.3.0/net/http.rb で定義されている module Net を
> 再オープン
> エラー部分を含むmethodをそのままコピー
> エラー部分を変更
> スクリプトより、require_relative ‘httpWrap’
> にて、スクリプトが raise で停止してしまうのを回避しました。
>
> もっとスマートな対応方法等ありますでしょうか?
>
> # cat httpWrap.rb
> module Net
> …
> def connect
> …
> s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
> begin
> TCPSocket.open(conn_address, conn_port, @local_host, @local_
> port)
> rescue => e
> =begin # original
> raise e, "Failed to open TCP connection to " +
> "#{conn_address}:#{conn_port} (#{e.message})"
> =end
> # modified start
> puts "Failed to open TCP connection to " +
> "#{conn_address}:#{conn_port} (#{e.message})"
> return nil
> # modified end
> end
> }
> …
> private :connect
>
> def transport_request(req)
> …
> rescue => exception
> D "Conn close because of error #{exception}"
> @socket.close if @socket and not @socket.closed?
> # raise exception # original code
> end
> …
> end
>