em-simple_telnet.rb 错误'pause_and_wait_for_result'



对于MUD客户端,我使用telnet。

我正试图像Apache那样了解天气,但这个错误似乎很可能是我没有正确使用em-simple_telnet API。

错误:

thufir@dur:~/ruby$ 
thufir@dur:~/ruby$ ruby weather.rb 
{"host"=>"rainmaker.wunderground.com", "port"=>3000}
/home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/em-simple_telnet-0.0.14/lib/em-simple_telnet.rb:712:in `pause_and_wait_for_result': EventMachine::Protocols::SimpleTelnet::ConnectionFailed (EventMachine::Protocols::SimpleTelnet::ConnectionFailed)
    from /home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/em-simple_telnet-0.0.14/lib/em-simple_telnet.rb:246:in `connect'
    from /home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/em-simple_telnet-0.0.14/lib/em-simple_telnet.rb:191:in `block in new'
thufir@dur:~/ruby$ 

代码:

#!/usr/bin/env ruby
require 'rubygems'
require 'em-simple_telnet'
require 'yaml'
require 'pry'
require 'pp'
opts = YAML.load_file('params.yml')
pp opts
EM::P::SimpleTelnet.new(opts) do |host|
  puts host.cmd("ls -la")
end

这是一个与天气服务的telnet会话:

thufir@dur:~/ruby$ 
thufir@dur:~/ruby$ 
thufir@dur:~/ruby$ telnet rainmaker.wunderground.com 3000
Trying 38.102.137.140...
Connected to rainmaker.wunderground.com.
Escape character is '^]'.
------------------------------------------------------------------------------
*               Welcome to THE WEATHER UNDERGROUND telnet service!            *
------------------------------------------------------------------------------
*                                                                            *
*   National Weather Service information provided by Alden Electronics, Inc. *
*    and updated each minute as reports come in over our data feed.          *
*                                                                            *
*   **Note: If you cannot get past this opening screen, you must use a       *
*   different version of the "telnet" program--some of the ones for IBM      *
*   compatible PC's have a bug that prevents proper connection.              *
*                                                                            *
*           comments: jmasters@wunderground.com                              *
------------------------------------------------------------------------------
Press Return to continue:
Press Return for menu
or enter 3 letter forecast city code-- dca
Weather Conditions at 12:27 AM EDT on 29 Aug 2013 for Washington, DC.
Temp(F)    Humidity(%)    Wind(mph)    Pressure(in)    Weather
========================================================================
  75          89%         NE at 7       29.85      Overcast
Forecast for Washington, MD
1030 PM EDT Wed Aug 28 2013
.Rest of tonight...Cloudy. Scattered showers late this evening...
then isolated showers. Patchy fog. Lows around 70. South winds
around 5 mph...becoming east. Chance of rain 50 percent. 
.Thursday...Mostly cloudy in the morning...then becoming mostly
sunny. Patchy fog in the morning. Scattered sprinkles. Highs in the
lower 80s. Northeast winds 5 to 10 mph. 
.Thursday night...Partly cloudy. Scattered sprinkles in the evening.
Lows in the mid 60s. East winds around 5 mph...becoming north after
midnight. 
.Friday...Mostly sunny. Highs in the lower 80s. North winds around
5 mph...becoming southeast in the afternoon. 
.Friday night...Partly cloudy in the evening...then becoming mostly
cloudy. Lows in the upper 60s. South winds around 5 mph. 
.Saturday...Partly sunny. Highs in the mid 80s. 
.Saturday night...Mostly cloudy. Lows in the upper 60s. 
   Press Return to continue, M to return to menu, X to exit: x
Connection closed by foreign host.
thufir@dur:~/ruby$ 
thufir@dur:~/ruby$ 

好的,我读了你的第一个链接。上面写着:

MUD客户端是用于连接到MUD的计算机应用程序多人在线游戏的类型。一般来说,MUD客户端是一个缺少VT100终端模拟和执行telnet协商的能力

不幸的是,我认为您的telnet gem尝试进行telnet协商,然后在没有从MUD获得任何响应时超时。我不知道作为一个不遵循telnet协议的telnet客户端意味着什么。

您可以尝试使用套接字连接到MUD服务器。

查看em-simple_telnet的自述文件中的示例。我想你应该把它封装在对EventMachine.run{…}的调用中。

此外,在选项中使用:host和:port等键,而不是"host"one_answers"port"。

我想Patrik似乎已经回答了这个问题。结果如下:
thufir@dur:~/ruby$ 
thufir@dur:~/ruby$ ruby weather.rb 
{"host"=>"rainmaker.wunderground.com", "port"=>3000}
{:host=>"rainmaker.wunderground.com", :port=>"3000"}
"opts and opts2 are different, didn't realize that"
"seems to connect, just not sure yet how print server output"
SimpleTelnet: EventMachine reactor had been started independently. Won't stop it automatically.
#<EventMachine::Protocols::SimpleTelnet:0x9c25408
 @check_input_buffer_timer=nil,
 @connection_state=:connected,
 @fiber_resumer=
  #<Proc:0x9c2537c@/home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/em-simple_telnet-0.0.14/lib/em-simple_telnet.rb:374 (lambda)>,
 @input_buffer="",
 @input_rest="",
 @last_command=nil,
 @logged_in=2013-08-29 06:50:13 -0700,
 @signature=2,
 @telnet_options=
  {:host=>"rainmaker.wunderground.com",
   :port=>"3000",
   :prompt=>/[$%#>] z/n,
   :connect_timeout=>3,
   :timeout=>10,
   :wait_time=>0,
   :bin_mode=>false,
   :telnet_mode=>true,
   :output_log=>nil,
   :command_log=>nil,
   :login_prompt=>/[Ll]ogin[: ]*z/n,
   :password_prompt=>/[Pp]ass(?:word|phrase)[: ]*z/n,
   :username=>nil,
   :password=>nil,
   :SGA=>false,
   :BINARY=>false},
 @wait_time_timer=nil>
^C/home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine': Interrupt
    from /home/thufir/.rvm/gems/ruby-2.0.0-p247/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
    from weather.rb:9:in `<main>'
thufir@dur:~/ruby$ 
thufir@dur:~/ruby$ 

代码:

#!/usr/bin/env ruby
require 'rubygems'
require 'em-simple_telnet'
require 'yaml'
require 'pry'
require 'pp'

EventMachine.run do
opts2 = YAML.load_file('params.yml')
pp opts2
  opts = {
    host: "rainmaker.wunderground.com",
    port: "3000",
  }

pp opts
pp "opts and opts2 are different, didn't realize that"
pp "seems to connect, just not sure yet how print server output"
EM::P::SimpleTelnet.new(opts) do |host|
  pp host
end
end

没有正确使用API,没有意识到。

最新更新