1

我对 net/http 的 http 请求有疑问...

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

在步骤“#Create the video object”之前它可以完美运行。最后一个“response = http.request(req)”命令引发以下错误->

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

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

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
4

1 回答 1

0

尝试调用完成

http.finish

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

于 2012-05-01T11:19:07.653 回答