开放失败:
irb(main):001:0> require 'open-uri'
=> true
irb(main):002:0> open("http://www.google.com")
RuntimeError: Non-HTTP proxy URI:
from /usr/lib/ruby/1.8/open-uri.rb:203:in `open_http'
from /usr/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
from /usr/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
from /usr/lib/ruby/1.8/open-uri.rb:162:in `catch'
from /usr/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
from /usr/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
from /usr/lib/ruby/1.8/open-uri.rb:518:in `open'
from /usr/lib/ruby/1.8/open-uri.rb:30:in `open'
from (irb):2
Run Code Online (Sandbox Code Playgroud)
但是,wget()有效......
root@pierr-desktop:/work/web/yy# wget www.google.com
--2010-11-14 20:00:39-- http://www.google.com/
Resolving www.google.com... 72.14.203.104, 72.14.203.99
Connecting to www.google.com|72.14.203.104|:80... connected.
HTTP request sent, awaiting response... 302 Found
.........
2010-11-14 20:00:40 (47.7 KB/s) - `index.html' saved [9097]
Run Code Online (Sandbox Code Playgroud)
我是否必须设置代理但我不知道正确的代理信息..
我正在尝试使用OpenURI从S3下载文件,然后将其保存在本地,以便我可以将文件作为附件发送给ActionMailer.
奇怪的事情正在发生.正在下载和附加的图像已损坏,图像的底部缺失.
这是代码:
require 'open-uri'
open("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}", "wb") do |file|
source_url = a.authenticated_url()
io = open(URI.parse(source_url).to_s)
file << io.read
attachments[a.attachment_file_name] = File.read("#{Rails.root.to_s}/tmp/#{a.attachment_file_name}")
end
Run Code Online (Sandbox Code Playgroud)
a 是ActionMailer的附件.
有任何想法吗?我真的很感激你的想法,因为我一直在用这头撞墙.
只要没有远程错误,当前代码就可以工作:
def get_name_from_remote_url
cstr = "http://someurl.com"
getresult = open(cstr, "UserAgent" => "Ruby-OpenURI").read
doc = Nokogiri::XML(getresult)
my_data = doc.xpath("/session/name").text
# => 'Fred' or 'Sam' etc
return my_data
end
Run Code Online (Sandbox Code Playgroud)
但是,如果远程URL超时或什么都不返回怎么办?例如,我如何检测到并返回nil?
而且,Open-URI是否提供了一种方法来定义放弃前等待的时间?当用户等待响应时调用此方法,那么我们如何在放弃之前设置最大timeoput时间并告诉用户"抱歉我们尝试访问的远程服务器现在不可用"?
当尝试open()远程图像时,有些人返回,StringIO而其他人则返回File......我该File怎么强迫?
data = open("http://graph.facebook.com/61700024/picture?type=square")
=> #<StringIO:0x007fd09b013948>
data = open("http://28.media.tumblr.com/avatar_7ef57cb42cb0_64.png")
=> #<StringIO:0x007fd098bf9490>
data = open("http://25.media.tumblr.com/avatar_279ec8ee3427_64.png")
=> #<File:/var/folders/_z/bb18gdw52ns0x5r8z9f2ncj40000gn/T/open-uri20120229-9190-mn52fu>
Run Code Online (Sandbox Code Playgroud)
我正在使用Paperclip来保存远程图像(存储在S3中),所以基本上想做:
user = User.new
user.avatar = open(url)
user.save
Run Code Online (Sandbox Code Playgroud) 我正在尝试将Flickr API中的图像加载到Ruby on Rails应用程序中,但我在JSON.parse()行上获得了"Unexpected Token".
我在这里找到了另一个响应,其中返回的JSON将双引号转义出来,解决方案是将.gsub事件添加到最后,但我仍然收到错误.
谁知道问题是什么?
def add
@jsonresults = open("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=bb398c11934abb6d51bdd720020f6a4a&per_page=1&page=1&format=json&nojsoncallback=1").read
@images = JSON.parse(@jsonresults.to_json.gsub('\"', '"'))
end
Run Code Online (Sandbox Code Playgroud)
错误:
JSON::ParserError in ImagesController#add
757: unexpected token at '"{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}"'
Run Code Online (Sandbox Code Playgroud) 我的代码中有一个函数,该函数接受代表图像url的File字符串,并根据该字符串创建对象,并将其附加到Tweet。这似乎有90%的时间有效,但偶尔会失败。
require 'open-uri'
attachment_url = "https://s3.amazonaws.com/FirmPlay/photos/images/000/002/443/medium/applying_too_many_jobs_-_daniel.jpg?1448392757"
image = File.new(open(attachment_url))
Run Code Online (Sandbox Code Playgroud)
如果我运行上面的代码,它将返回TypeError: no implicit conversion of StringIO into String。如果我改变open(attachment_url)了open(attachment_url).read我会得到ArgumentError: string contains null byte。我还尝试像这样从文件中删除空字节,但这也没有区别。
image = File.new(open(attachment_url).read.gsub("\u0000", ''))
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试使用其他图像(例如下面的图像)尝试原始代码,则可以正常工作。它File按预期返回一个对象:
attachment_url = "https://s3.amazonaws.com/FirmPlay/photos/images/000/002/157/medium/mike_4.jpg"
Run Code Online (Sandbox Code Playgroud)
我以为可能与原始网址中的参数有关,所以我删除了这些参数,但这没什么区别。如果我在Chrome中打开图像,则看起来效果很好。
我不确定我在这里缺少什么。我该如何解决这个问题?
谢谢!
更新资料
这是我的应用程序中的工作代码:
filename = self.attachment_url.split(/[\/]/)[-1].split('?')[0]
stream = open(self.attachment_url)
image = File.open(filename, 'w+b') do |file|
stream.respond_to?(:read) ? IO.copy_stream(stream, file) : file.write(stream)
open(file)
end
Run Code Online (Sandbox Code Playgroud)
Jordan的答案有效,除了调用File.new返回一个空File对象,而File.open返回一个File包含来自的图像数据的对象stream。
我不知道如何使用 open-uri 和主动代理从 URL 下载数据。
我使用了这段代码:
proxy = Net::HTTP::Proxy("??????????????????????")
proxy.start('http://www.example.com') { |http|
puts open(strURL).read
}
Run Code Online (Sandbox Code Playgroud)
但 Ruby 返回错误: c:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:in `initialize': 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应。- 连接(2) (Errno::ETIMEDOUT)
怎么了?(我不想使用 HTTP)(
查阅了大量文档并尝试了猴子修补。不知道如何强制获得适当的证书,也不知道从哪里获得它们。常规 http 页面一切正常。使用 Ruby 1.9.3
这是堆栈跟踪:
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `connect': SSL_connect returned=1 e
rrno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL
::SSL::SSLError)
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `block in connect'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `connect'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:756:in `do_start'
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745: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:776: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:678:in `open'
from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:33:in `open'
Run Code Online (Sandbox Code Playgroud) 我正在使用 Rails 4.2.3 和 Nokogiri 从网站获取数据。当我没有从服务器得到任何响应时,我想执行一个操作,所以我有:
begin
content = open(url).read
if content.lstrip[0] == '<'
doc = Nokogiri::HTML(content)
else
begin
json = JSON.parse(content)
rescue JSON::ParserError => e
content
end
end
rescue Net::OpenTimeout => e
attempts = attempts + 1
if attempts <= max_attempts
sleep(3)
retry
end
end
Run Code Online (Sandbox Code Playgroud)
请注意,这与从服务器获取 500 不同。我只想在完全没有响应时重试,要么是因为我没有得到 TCP 连接,要么是因为服务器无法响应(或其他一些导致我没有得到任何响应的原因)。除了我的情况之外,是否有更通用的方法来考虑这种情况?我觉得还有很多我没有想到的其他异常类型。
我正在尝试下载一组图像,并提供它们的 URL。某些 URL 重定向到包含空格的 URL,这会导致 OpenURI 引发错误。
即我提供了http://www.example.com/upload/comercial%20(2).jpg重定向到https://www.example.com/upload/comercial (2).jpg. 这会在负责下载的代码中引发错误:
url = 'http://www.example.com/upload/comercial%20(2).jpg'
download = open(url, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, allow_redirections: :all})
OpenURI::HTTPError: 302 Redirect (Invalid Location URI)
Run Code Online (Sandbox Code Playgroud)
有没有办法让 OpenURI 理解重定向?
观察: 在对空格进行编码后,OpenURI 可以正确处理生成的重定向 URL,如下所示:
redirected = 'https://www.example.com/upload/comercial (2).jpg'
encoded = URI.escape(redirected)
# https://www.example.com/upload/comercial%20(2).jpg
download = open(encoded, {ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, allow_redirections: :all})
# Success
Run Code Online (Sandbox Code Playgroud)
显然,在调用之前,重定向 URL 是未知的open()。