Catching timeout errors with ruby mechanize

前端 未结 2 797
忘掉有多难
忘掉有多难 2021-01-02 08:27

I have a mechanize function to log me out of a site but on very rare occasions it times me out. The function involves going to a specific page, and then clicking on a logout

相关标签:
2条回答
  • 2021-01-02 08:55

    Instead of retrying some timeouts on some mechanize requests I think you'd better set Mechanize::HTTP::Agent::read_timeout attribute to a reasonable amount of seconds like 2 or 5, anyway one that prevent timeouts errors for this request.

    Then, it seem's that your log out procedure only required access to a simple HTTP GET request. I mean there is no form to fill in so no HTTP POST request. So if I were you, I would prefere inspected the page source code (Ctrl+U with Firefox or Chrome) in order to identify the link which is reached by your agent.click(page.link_with(:text => /Log Out/i)) It should be faster because these type of pages are usually blank and Mechanize will not have to load a full html web page in memory.

    Here is the code I would prefer use :

    def logmeout(agent)
      begin
      agent.read_timeout=2  #set the agent time out
      page = agent.get('http://www.example.com/logout_url.php')
      agent.history.pop()   #delete this request in the history
      rescue Timeout::Error 
        puts "Timeout!"
        puts "read_timeout attribute is set to #{agent.read_timeout}s" if !agent.read_timeout.nil?
        #retry      #retry is no more needed
      end
    end
    

    but you can use your retry function too :

    def trythreetimes
      tries = 0
      begin
      yield
      rescue Exception => e  
      tries += 1
      puts "Error: #{e.message}"
      puts "Trying again!" if tries <= 3
      retry if tries <= 3
      puts "No more attempt!"
      end
    end
    
    def logmeout(agent)
      trythreetimes do
      agent.read_timeout=2  #set the agent time out
      page = agent.get('http://www.example.com/logout_url.php')
      agent.history.pop()       #delete this request in the history
      end
    end
    

    hope it helps ! ;-)

    0 讨论(0)
  • 2021-01-02 09:01

    Using mechanize 1.0.0 I got this problem from a different source of error.

    In my case I was blocked by proxy and then SSL. This worked for me:

    ag = Mechanize.new
    ag.set_proxy('yourproxy', yourport)
    ag.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    ag.get( url )
    
    0 讨论(0)
提交回复
热议问题