`
tcwt008
  • 浏览: 11020 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

ruby 作为 Telnet、客户端

阅读更多
[size=x-large][color=olive]作为 Telnet、客户端
  想要连接到一个telent服务器或者使用telent得到某些类型的服务器的低级访问。
  使用ruby 标准 NET::Telent模块。
  如下代码使用Telent 模仿一个HTTp客户端,他发送一条原始的HTTP请求给 http://www。oreilly。com

处的web 服务器,从web 服务器得到的没块数据被传递进一个代码块, 并且大小被添加到一个计数器。

最终WEB 服务器停止发送数据, telent会话终止。
require 'net/telnet'

	webserver = Net::Telnet::new('Host' => 'www.oreilly.com',
	                             'Port' => 80,
	                             'Telnetmode' => false)

	size = 0
	webserver.cmd("GET / HTTP/1.1\nHost: www.oreilly.com\n") do |c|
	  size += c.size
	  puts "Read #{c.size} bytes; total #{size}"
	end
	# Read 1431 bytes; total 1431
	# Read 1434 bytes; total 2865
	# Read 1441 bytes; total 4306
	# Read 1436 bytes; total 5742
	# …
	# Read 1430 bytes; total 39901
	# Read 2856 bytes; total 42757
	# /usr/lib/ruby/1.8/net/telnet.rb:551:in 'waitfor':
	#   timed out while waiting for more data (Timeout::Error)[/color][/size]
1
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics