HTTP request error: name or service not known - ruby

I am trying to make HTTP requests via Ruby. When running the following code:
require "net/http"
require "uri"
uri = URI.parse("http://google.com/")
# Will print response.body
Net::HTTP.get_print(uri)
I get the following error:
SocketError: initialize: name or service not known
from org/jruby/ext/socket/RubyTCPSocket.java:129:in `initialize'
from org/jruby/RubyIO.java:1179:in `open'
from c:/jruby-1.7.3/lib/ruby/1.9/net/http.rb:762:in `connect'
from org/jruby/ext/timeout/Timeout.java:105:in `timeout'
from c:/jruby-1.7.3/lib/ruby/1.9/net/http.rb:762:in `connect'
from c:/jruby-1.7.3/lib/ruby/1.9/net/http.rb:755:in `do_start'
from c:/jruby-1.7.3/lib/ruby/1.9/net/http.rb:744:in `start'
from c:/jruby-1.7.3/lib/ruby/1.9/net/http.rb:454:in `get_response'
from c:/jruby-1.7.3/lib/ruby/1.9/net/http.rb:412:in `get_print'
from (irb):29:in `evaluate'
from org/jruby/RubyKernel.java:1066:in `eval'
from org/jruby/RubyKernel.java:1409:in `loop'
from org/jruby/RubyKernel.java:1174:in `catch'
from org/jruby/RubyKernel.java:1174:in `catch'
from c:/jruby-1.7.3/bin/irb:13:in `(root)'
This is probably proxy related, but I am not sure. How can I fix this problem?

Ruby will automatically use the HTTP proxy as specified in your environment variables.
EXPORT http_proxy=http://foo.bar:8080/
ruby your_http_script.rb

Same thing happened to me. No proxy was in use.
The reason was that I was not running as root and /etc/resolv.conf didn't have read permissions to anyone other than root. Adding read permissions solved the host resolving problem.

Related

Download multiple XML files

I'm writing an application to parse XML. I have to obtain data from one XML file, and then in a loop I have to open another XML file.
The code looks like this:
$doc = Nokogiri::XML(open('myxmladress'))
$doc.xpath('//job').each do |job|
if job.xpath('name').text.include?('joe')
$doc2 = Nokogiri::XML(open('myxmladress_for_joe'))
end
end
I believe that I cannot have multiple HTTP connections open.
Can I simply download the whole file instead of using
$doc Nokogiri::XML(open('myxmladress'))
or is there any way to close the Nokogiri HTTP connection?
What is more I'm downloading it by https.
My error:
in `open_http': 500 Server Error (OpenURI::HTTPError)
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:210:in `catch'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:717:in `open'
from /home/nagios/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/open-uri.rb:35:in `open'
from jenkins_auth.rb:97:in `block (2 levels) in combine_partial_results'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:187:in `block in each'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `upto'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `each'
from jenkins_auth.rb:89:in `block in combine_partial_results'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:187:in `block in each'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `upto'
from /home/nagios/.rvm/gems/ruby-2.3.0/gems/nokogiri-1.6.7.2/lib/nokogiri/xml/node_set.rb:186:in `each'
from jenkins_auth.rb:86:in `combine_partial_results'
from jenkins_auth.rb:130:in `get_tests_for_job'
from jenkins_auth.rb:137:in `<main>'
You are using OpenUri to fetch a document from a URL. I am pretty sure that this doesn't leave any open connection, but reads the document into an IO like object, kind of like a file.
Your problem seems to be that the server has had an internal error.

"No connection could be made" to GitHub

I am trying to get this run to my test organization created on github.com, but got this error. Can anyone tell me what's wrong?
C:\Workspace\Ruby>set GITHUB_TOKEN=xxx
C:\Workspace\Ruby>set GITHUB_API_ENDPOINT=http://api.github.com
C:\Workspace\Ruby>ruby team_audit.rb HudsonsBay
C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:879:in `initialize': No connection could be made because the target machine actively refused it. - connect(2) for "api.github.com" port 80 (Faraday::Conn
ectionFailed)
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:879:in `open'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:879:in `block in connect'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/timeout.rb:75:in `timeout'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:878:in `connect'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:863:in `do_start'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:852:in `start'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:1375:in `request'
from C:/Tools/Ruby21-x64/lib/ruby/2.1.0/net/http.rb:1133:in `get'
from C:/Tools/Ruby21-x64/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/adapter/net_http
.rb:80:in `perform_request'
from C:/Tools/Ruby21-x64/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/adapter/net_http
.rb:40:in `block in call'
from C:/Tools/Ruby21-x64/lib/ruby/gems/2.1.0/gems/faraday-
C:\Workspace\Ruby>
I tried to make the same request and got the same error.
after changing the URL from http://api.github.com into https://api.github.com it working.

ruby: ECONNREFUSED from http.rb

I am trying to parse an rss feed with Ruby. This is my (working) code, with the actual feed url:
include 'rss'
myfeed = RSS::Parser.parse('http://allmusicnews.altervista.org/blog/feed/', false)
If I run it from my development computer (Mac OS 10.9), all works fine. If I run it from my server (Debian 7), all i get is:
/home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:879:in `initialize': Connection refused - connect(2) for "allmusicnews.altervista.org" port 80 (Errno::ECONNREFUSED)
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:879:in `open'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/timeout.rb:74:in `timeout'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:878:in `connect'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/net/http.rb:852:in `start'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:318:in `open_http'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:736:in `buffer_open'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:211:in `block in open_loop'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:209:in `catch'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:209:in `open_loop'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:150:in `open_uri'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:716:in `open'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/open-uri.rb:724:in `read'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:103:in `normalize_rss'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:89:in `initialize'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:76:in `new'
from /home/daniele/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rss/parser.rb:76:in `parse'
This does not happen with other rss feeds. example: feeds.blogo.it/soundsblog/it seems working well on both platforms.
I am using the same Ruby version (2.2.0), installed with RVM on both the computers. Same gems too.
Any clue, at least about what to look for?
ps: it may seem something related to connection timeout, but as I run my script suddenly I see the error message above, there's no idle waiting time...
Please ignore my question. I was shooting to the wrong target.
Actually I'm afraid to have some blacklistish problem with my production machine. I cannot connect to altervista.org server in any way (lynx, telnet), I keep getting "connection refused error", so there's nothing to do with my Ruby code...

Error when running net/http in ruby via the terminal

Running a Net::HTTP post request in ruby. I am using the terminal in Ubuntu and receive this error message:
SocketError: getaddrinfo: Name or service not known
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `open'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
from /usr/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
from /usr/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from /usr/lib/ruby/1.9.1/net/http.rb:762:in `connect'
from /usr/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
from /usr/lib/ruby/1.9.1/net/http.rb:744:in `start'
from /usr/lib/ruby/1.9.1/net/http.rb:1284:in `request'
from /usr/lib/ruby/1.9.1/net/http.rb:1307:in `send_entity'
from /usr/lib/ruby/1.9.1/net/http.rb:1096:in `post'
from (irb):356
from /usr/bin/irb:12:in `<main>'
Running the following code:
uri = ##url
http = Net::HTTP.new(uri)
response = http.post('/', ##str)
puts response
"Name or service not known" is a socket level error that is thrown when you cannot resolve a given domain name or are specifying an IP address that cannot be connected to. No other information will be available without posting the URL and string you are trying to post.

With Nokogiri i am getting error "initialize': getaddrinfo: No such host is known. (SocketError)"

I worte the below code, just to start the Nokogiri to mee the requirement and when I ran it for testing got the error:
D:\WIPData\Ruby\Scripts>Nokogiri.rb
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `initialize': getaddrinfo: No such
host is known. (SocketError)
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `open'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:762:in `connect'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:744:in `start'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:775:in `buffer_open'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:677:in `open'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:33:in `open'
from D:/WIPData/Ruby/Scripts/Nokogiri.rb:6:in `<main>'
D:\WIPData\Ruby\Scripts>
CODE
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML::Document for the page we’re interested in...
doc = Nokogiri::HTML(open('http://www.google.co.in'))
Why so and how to fix the same,please advice me.
It appears you need to configure a proxy. Find out what the proxy URL/Port is for your organization (and whether there needs to be authentication). You may be able to view this information from your browser configuration. In order to use it with your Ruby code, you need to set the HTTP_PROXY environment variable.
You can set it in Ruby code:
ENV['HTTP_PROXY'] = 'http://hostname:port'
or if you need authentication:
ENV['HTTP_PROXY'] = 'http://username:password#hostname:port'
A more permanent solution is to set HTTP_PROXY in your system environment variables.

Resources