Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby:强制打开uri返回IPv4地址_Ruby On Rails_Ruby_Open Uri - Fatal编程技术网

Ruby on rails Ruby:强制打开uri返回IPv4地址

Ruby on rails Ruby:强制打开uri返回IPv4地址,ruby-on-rails,ruby,open-uri,Ruby On Rails,Ruby,Open Uri,在Rails应用程序中,我们有一个控制器操作,它打开一个外部URL,并将其作为JSON返回给前端使用 今天,我犯了以下错误: Errno::EAFNOSUPPORT:协议不支持地址系列-套接字(2) 我们的devops建议,如果该地址返回IPv6响应以及IPv4响应,则这将是错误的。我们不支持IPv6,所以这可以解释它 以下是触发请求的方法: def medline_response open("http://apps.nlm.nih.gov/medlineplus/service

在Rails应用程序中,我们有一个控制器操作,它打开一个外部URL,并将其作为JSON返回给前端使用

今天,我犯了以下错误:

Errno::EAFNOSUPPORT:协议不支持地址系列-套接字(2)

我们的devops建议,如果该地址返回IPv6响应以及IPv4响应,则这将是错误的。我们不支持IPv6,所以这可以解释它

以下是触发请求的方法:

def medline_response
  open("http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=#{codesys}&mainSearchCriteria.v.c=#{code}")
end
实际上,来自该服务器主机的响应:

$ host apps.nlm.nih.gov
apps.nlm.nih.gov is an alias for apps.wip.nlm.nih.gov.
apps.wip.nlm.nih.gov has address 130.14.16.117
apps.wip.nlm.nih.gov has IPv6 address 2607:f220:41e:1016::117
网站上没有任何关于IP版本的内容。是否有一个参数可以传递到
open
方法中,强制它读取IPv4,或者用另一种方法来处理这个问题

编辑 当我尝试从控制台打开google时,我得到以下信息:

[18] pry(main)> require 'open-uri'
=> false
[19] pry(main)> open 'http://google.com'   
=> #<File:/var/folders/hq/7_rgkt4565q728y91dtqzs2r2wxb1y/T/open-uri20130913-50258-1af4zhv>
OS X 10.7.5

编辑3 这是每个人最喜欢的一种错误:间歇性错误。下面是在
openuri
对象上调用read时发生的情况。我在本地和所有3个应用服务器上都得到了相同的输出

[22] pry(main)> open("http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=238.4").read
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<feed xml:base=\"http://apps.nlm.nih.gov/medlineplus/services/\" xml:lang=\"en\" xmlns=\"http://www.w3.org/2005/Atom\" xmlns:v3=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><title type=\"text\">MedlinePlus Connect</title><subtitle type=\"text\">MedlinePlus Connect results for ICD-9-CM 238.4</subtitle><author><name>U.S. National Library of Medicine</name><uri>http://www.nlm.nih.gov</uri></author><updated type=\"text\">2013-09-13T12:09:48Z</updated><category scheme=\"REDS_MT010001UV\" term=\"MATCHED\"><v3:mainSearchCriteria classCode=\"OBS\" moodCode=\"DEF\" xmlns:v3=\"urn:hl7-org:v3\"><v3:code code=\"KSUBJ\" codeSystem=\"2.16.840.1.113883.5.4\" xmlns:v3=\"urn:hl7-org:v3\"/><v3:value code=\"238.4\" codeSystem=\"2.16.840.1.113883.6.103\" displayName=\"\" xmlns:v3=\"urn:hl7-org:v3\"/></v3:mainSearchCriteria><v3:informationRecipient typeCode=\"IRCP\" xmlns:v3=\"urn:hl7-org:v3\"><v3:patient classCode=\"PAT\" xmlns:v3=\"urn:hl7-org:v3\"/></v3:informationRecipient></category><id></id><entry><title>Bone Marrow Diseases</title><link href=\"http://www.nlm.nih.gov/medlineplus/bonemarrowdiseases.html\" rel=\"alternate\"/><id>tag: nlm.nih.gov, 2013-13-09:/medlineplus/bonemarrowdiseases.html</id><updated>2013-09-13T12:09:48Z</updated><summary type=\"html\">&lt;p&gt;Bone marrow is the spongy tissue inside some of your bones, such as your hip and thigh bones. It contains immature cells, called stem cells. The stem cells can develop into the red blood cells that carry oxygen through your body, the white blood cells that fight infections, and the platelets that help with blood clotting. &lt;/p&gt;&#xd;&#xd;&lt;p&gt;If you have a bone marrow disease, there are problems with the stem cells or how they develop. &lt;a href=\"http://www.nlm.nih.gov/medlineplus/leukemia.html\"&gt;Leukemia&lt;/a&gt; is a cancer in which the bone marrow produces abnormal white blood cells. With &lt;a href=\"http://www.nlm.nih.gov/medlineplus/aplasticanemia.html\"&gt;aplastic anemia&lt;/a&gt;, the bone marrow doesn't make red blood cells. Other diseases, such as &lt;a href=\"http://www.nlm.nih.gov/medlineplus/lymphoma.html\"&gt;lymphoma&lt;/a&gt;, can spread into the bone marrow and affect the production of blood cells.  Other causes of bone marrow disorders include your genetic makeup and environmental factors.&lt;/p&gt; &#xd;&#xd;&lt;p&gt;Symptoms of bone marrow diseases vary. Treatments depend on the disorder and how severe it is. They might involve medicines, blood transfusions or a &lt;a href=\"http://www.nlm.nih.gov/medlineplus/bonemarrowtransplantation.html\"&gt;bone marrow transplant&lt;/a&gt;. &lt;/p&gt;&#xd; &lt;p class=\"NLMrelatedLinks\"&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003658.htm\"&gt;Bone marrow aspiration&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003934.htm\"&gt;Bone marrow biopsy&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003682.htm\"&gt;Bone marrow culture&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003009.htm\"&gt;Bone marrow transplant&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/patientinstructions/000010.htm\"&gt;Bone marrow transplant - discharge&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/000589.htm\"&gt;Polycythemia vera&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=\"http://www.nlm.nih.gov/medlineplus/ency/article/000531.htm\"&gt;Primary myelofibrosis&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;</summary></entry></feed>  \n\n"
[22]撬动(主)>打开(“http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm?mainSearchCriteria.v.cs=2.16.840.1.113883.6.103&mainSearchCriteria.v.c=238.4)读
=> “\n用于ICD-9-CM 238.4U.S的MedLinePlus连接MedLinePlus连接结果。国立医学图书馆icinehttp://www.nlm.nih.gov2013-09-13T12:09:48ZBone 骨髓疾病Stag:nlm.nih.gov,2013-13-09:/medlineplus/bonemarrowdiseases.html2013-09-13T12:09:48zp骨髓是一些骨骼(如髋骨和大腿骨)内的海绵组织。它含有未成熟细胞,称为干细胞。干细胞可以发育成携带氧气通过身体的红细胞、抵抗感染的白细胞和帮助血液凝固的血小板/p&xd&#除息的;若你们有骨髓疾病,那个么干细胞或它们是如何发育的就有问题了。a href=\”http://www.nlm.nih.gov/medlineplus/leukemia.html\“白血病/a是一种骨髓产生异常白细胞的癌症。使用a href=\”http://www.nlm.nih.gov/medlineplus/aplasticanemia.html\“再生障碍性贫血/a,骨髓不产生红细胞。其他疾病,如a href=\”http://www.nlm.nih.gov/medlineplus/lymphoma.html\“淋巴瘤/a,可扩散到骨髓并影响血细胞的生成。骨髓疾病的其他原因包括你的基因构成和环境因素。/p
&#除息的;骨髓疾病的症状各不相同。治疗取决于疾病的严重程度。可能涉及药物、输血或a href=\”http://www.nlm.nih.gov/medlineplus/bonemarrowtransplantation.html\“骨髓移植/a/p&xd;p类=\“NLMrelatedLinks\”ullia href=\“http://www.nlm.nih.gov/medlineplus/ency/article/003658.htm\“骨髓穿刺/a/lilia href=\”http://www.nlm.nih.gov/medlineplus/ency/article/003934.htm\“骨髓活检/a/lilia href=\”http://www.nlm.nih.gov/medlineplus/ency/article/003682.htm\“骨髓培养/a/lilia href=\"http://www.nlm.nih.gov/medlineplus/ency/article/003009.htm\“骨髓移植/a/lilia href=\”http://www.nlm.nih.gov/medlineplus/ency/patientinstructions/000010.htm\“骨髓移植-出院/a/lilia href=\”http://www.nlm.nih.gov/medlineplus/ency/article/000589.htm\“真性红细胞增多症/a/lilia href=\”http://www.nlm.nih.gov/medlineplus/ency/article/000531.htm\“原发性骨髓纤维化/a/li/ul/p\n\n”

也就是说,它以字符串的形式返回
HTML
,正如您所期望的那样。

似乎您需要更新的ruby,请参阅

您可以“强制”打开uri以使用ip,例如:

require 'open-uri'
require 'socket'

url = "google.com"
ip = IPSocket::getaddress(url)

open("http://#{url}") do |f|
  puts "i'm using url #{url} #{f.base_uri}"
end
open("http://#{ip}") do |f|
  puts "i'm using ip #{ip} #{f.base_uri}"
end

如果你用google.com来做,会发生什么?它也有一个IPv6地址。(当我做hostapps.nlm.nih.gov时,我得到了和你一样的东西)我不相信问题出在OpenURI或IPv6上,因为如果他们是问题对,我们早就听说了。OpenURI从处理该URI的主机返回的流中读取,OpenURI将文件句柄返回到StreamIO或其后台处理内容的文件。尝试在MedLine上执行的
打开
阅读
。另外,您使用的是什么Ruby、OpenURI和操作系统?请参阅编辑。我已经添加了这个信息。OpenURI是Ruby核心的一部分,所以它应该是相同的版本,对吗?如果它是间歇性的,并且您的代码只在一台主机上运行,我会指向网络的某个地方。如果您在多台机器上运行,那么在这些代码的其中一个副本下面可能有不同的配置。如果ip上有多个域,那么这种开放式(“http://{ip}”)是无用的,这在当今相当普遍。在这种情况下,您需要使用完整的Net::HTTP而不是简单的开放uri。
require 'open-uri'
require 'socket'

url = "google.com"
ip = IPSocket::getaddress(url)

open("http://#{url}") do |f|
  puts "i'm using url #{url} #{f.base_uri}"
end
open("http://#{ip}") do |f|
  puts "i'm using ip #{ip} #{f.base_uri}"
end