kenlistian

勤学多思

  IT博客 :: 首页 :: 新随笔 ::  :: 聚合  :: 管理 ::
  207 随笔 :: 0 文章 :: 23 评论 :: 0 Trackbacks

require "net/http"
require "net/https"
require "erb"
require "singleton"

PANEL = 'panel.dreamhost.com'
PATH = '/index.cgi'
USERAGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'

class Panel
include Singleton
def Panel.login(webid, password)
unless @headers.nil?
raise ScriptError, "You should already be logged in!"
end
@http = Net::HTTP.new(PANEL, 443)
@http.use_ssl = true
resp, data = @http.get2(PATH, {'User-Agent' => USERAGENT})
cookie = resp.response['set-cookie'].split('; ')[0]
#主要它应付了有些网站需要cookie的要求
data = "Nscmd=Nlogin&username=#{ERB::Util.url_encode(webid)}&password=#{ERB::Util.url_encode(password)}&submit=Log%20In"
@headers = {
'Cookie' => cookie,
'Referer' => 'https://'+PANEL+PATH,
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => USERAGENT
}

puts "headers = " + @headers.inspect
resp, data = @http.post2(PATH, data, @headers)

# Output on the screen -> we should get either a 302 redirect (after a successful login) or an error page
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data
end
end

调用

Panel.login("kenlistian", "password")

摘自老外的片段,学习用。

posted on 2008-03-25 22:56 kenlistian 阅读(311) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。