#!/usr/bin/ruby -w require "xmlrpc/client" require "fileutils" # Initiate an aria2 xml-rpc server command = "aria2c --disable-ipv6=true --enable-xml-rpc --xml-rpc-listen-port=6801" FileUtils.mkdir_p('/tmp/aria2') pid = Process.spawn(command, :chdir=>'/tmp/aria2', STDOUT=>'/dev/null', STDIN=>'/dev/null') puts "Process aria2 spawned (pid %d)" % pid begin # Connect to aria2 sleep 3 # quick-n-dirty hack to make sure the server is ready to serve puts "Connect to server..." client = XMLRPC::Client.new3({:host => '127.0.0.1', :port => 6801, :path => '/rpc'}) version = client.call('aria2.getVersion')['version'] puts "Version: aria2 %s" % version # Add a very small file to download uri = 'http://www.google.com/favicon.ico' puts "Add very small file to download: %s" % uri gid = client.call('aria2.addUri', [uri]) puts "Files:" p client.call('aria2.getFiles', gid) while sleep 1 status = client.call('aria2.tellStatus', gid) break if status['status'] == 'complete' p status end puts "" puts "List of files for finished download:" p client.call('aria2.getFiles', gid) puts "" rescue p $! ensure # Kill server puts "Send kill signal TERM to pid %d" % pid Process.kill('TERM', pid) puts "" end