太多打开的文件 - 带有net / http的socket(2)

时间:2012-05-01 07:30:27

标签: ruby-on-rails ruby sockets

我的net / http ...

的http请求有问题

我编写了一个与dailymotion api交互的ruby脚本。 该脚本将上传视频“test.flv”。 基本上它包含四个请求。

直到步骤“#Create the video object”才能完美运行。 最后引发了以下错误 “response = http.request(req)”命令 - >

Errno :: EMFILE:打开的文件太多 - socket(2)

这是代码,thx任何建议......

require 'net/http'
require 'curb'
require 'json'


# Authenticate the user
url = URI.parse( 'https://api.dailymotion.com/oauth/token' )
req = Net::HTTP::Post.new(url.path)
req.set_form_data({ 'grant_type' => 'password',
           'client_id' => 'my_client_id',
           'client_secret' => 'my_client_secret',
           'username' => 'myusername',
           'password' => 'mypassword'
         })
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request(req)

access_token = JSON.parse( response.body )['access_token']
access_url = 'https://api.dailymotion.com/file/upload?access_token=' + access_token



# Get an upload URL
url = URI.parse( access_url )
req = Net::HTTP::Get.new( url.request_uri )
http = Net::HTTP.new( url.host, url.port )
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request( req )

upload_url = JSON.parse( response.body )['upload_url']
progress_url = JSON.parse( response.body )['progress_url']


# Post the video
fields_hash = {}

post_data = fields_hash.map { |k, v| Curl::PostField.content(k, v.to_s) }
post_data << Curl::PostField.file('file', 'C:/test.flv')

c = Curl::Easy.new(upload_url)
c.multipart_form_post = true
c.http_post(post_data)

file_url = JSON.parse( c.body_str )['url']


# Create the video object
url = URI.parse( 'https://api.dailymotion.com/me/videos' )
req = Net::HTTP::Post.new(url.path)
req.set_form_data({ 'url' => file_url,
                'access_token' => access_token
         })
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
response = http.request(req)

puts response.body

1 个答案:

答案 0 :(得分:0)

尝试调用完

http.finish

在响应= http.request(req)之后。