Download a zip file through Net::HTTP

前端 未结 2 1577
醉酒成梦
醉酒成梦 2021-01-13 22:03

I am trying to download the latest.zip from WordPress.org using Net::HTTP. This is what I have got so far:

Net::HTTP.start(\"wordpress.org/\") { |http|
  res         


        
相关标签:
2条回答
  • 2021-01-13 22:23

    My first question is why use Net::HTTP, or code to download something that could be done more easily using curl or wget, which are designed to make it easy to download files?

    But, since you want to download things using code, I'd recommend looking at Open-URI if you want to follow redirects. Its a standard library for Ruby, and very useful for fast HTTP/FTP access to pages and files:

    require 'open-uri'
    
    open('latest.zip', 'wb') do |fo|
      fo.print open('http://wordpress.org/latest.zip').read
    end
    

    I just ran that, waited a few seconds for it to finish, ran unzip against the downloaded file "latest.zip", and it expanded into the directory containing their content.

    Beyond Open-URI, there's HTTPClient and Typhoeus, among others, that make it easy to open an HTTP connection and send queriers/receive data. They're very powerful and worth getting to know.

    0 讨论(0)
  • 2021-01-13 22:34

    NET::HTTP doesn't provide a nice way of following redirects, here is a piece of code that I've been using for a while now:

    require 'net/http'
    class RedirectFollower
      class TooManyRedirects < StandardError; end
    
      attr_accessor :url, :body, :redirect_limit, :response
    
      def initialize(url, limit=5)
        @url, @redirect_limit = url, limit
      end
    
      def resolve
        raise TooManyRedirects if redirect_limit < 0
    
        self.response = Net::HTTP.get_response(URI.parse(url))
    
        if response.kind_of?(Net::HTTPRedirection)      
          self.url = redirect_url
          self.redirect_limit -= 1
    
          resolve
        end
    
        self.body = response.body
        self
      end
    
      def redirect_url
        if response['location'].nil?
          response.body.match(/<a href=\"([^>]+)\">/i)[1]
        else
          response['location']
        end
      end
    end
    
    
    
    wordpress = RedirectFollower.new('http://wordpress.org/latest.zip').resolve
    puts wordpress.url
    File.open("latest.zip", "w") do |file|
      file.write wordpress.body
    end
    
    0 讨论(0)
提交回复
热议问题