0

对于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$ 
4

3 回答 3

1

好的,我阅读了您的第一个链接。它说:

MUD 客户端是用于连接到 MUD(一种多人在线游戏)的计算机应用程序。通常,MUD 客户端是一个非常基本的 telnet 客户端,缺少 VT100 终端仿真和执行 telnet 协商的能力

不幸的是,我认为您的 telnet gem 尝试进行 telnet 协商,然后在没有得到 MUD 的任何响应时超时。我不确定成为不遵循 telnet 协议的 telnet 客户端意味着什么。

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

于 2013-08-29T06:52:43.720 回答
1

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

此外,在选项中使用 :host 和 :port 之类的键,而不是“Host”和“Port”。

于 2013-08-29T09:35:39.073 回答
0

我想,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,没有意识到。

于 2013-08-29T13:47:39.390 回答