简体   繁体   中英

HTTP::ConnectionError & Errno::EHOSTUNREACH Errors in Rails App

I'm working on a Rails app and here are two important pieces of the error message I get when I try to seed data in my database:

  1. HTTP::ConnectionError: failed to connect: Operation timed out - SSL_connect

  2. Errno::ETIMEDOUT: Operation timed out - SSL_connect

Here is my code, where I'm pulling data from a file, and creating Politician objects:

politician_data.each do |politician|

    photo_from_congress = "https://theunitedstates.io/images/congress/original/" + politician["id"]["bioguide"] + ".jpg"

    HTTP.get(photo_from_congress).code == 200 ? image = photo_from_congress : image = "noPoliticianImage.png" 

    Politician.create(
        name: "#{politician["name"]["first"]} #{politician["name"]["last"]}",
        image: image
    )
end

I put in a pry, and the iteration works for the first loop, so the code is OK. After several seconds, the loop breaks, and I get that error, so I think it has something to do with the number of HTTP.get requests I'm making?

https://github.com/unitedstates/images is a Git repo. Perhaps that repo can't handle that many get requests?

I did some Google'ing and saw it may have something to do with "Request timed out" error? My having to set up a proxy servers? I'm a junior programmer so please be very specific when responding.

*EDIT TO ADD THIS :

I found this blurb on the site where I'm making get requests to cull photos ( https://github.com/unitedstates/images ), that may help?

Note: Our HTTPS permalinks are provided through CloudFlare's Universal SSL, which also uses "Flexible SSL" to talk to GitHub Pages' unencrypted endpoints. So, you should know that it's not an end-to-end encrypted channel, but is encrypted between your client use and CloudFlare's servers (which at least should dissociate your requests from client IP addresses).

by the way, using "Net::HTTP" instead of the "HTTP" Ruby gem worked. Instead of checking the status code, i just checked to see if the body contained key text:

photo_from_congress = "https://theunitedstates.io/images/congress/original/" + politician["id"]["bioguide"] + ".jpg" 

photo_as_URI = URI(photo_from_congress)

Net::HTTP.get_response(photo_as_URI ).body.include?("File not found") ? image = "noPoliticianImage.png" : image = photo_from_congress

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM