Monday, June 1, 2009

RTorrent Remote Control

My preferred bittorrent client is rtorrent. I like it because the text user interface makes it convenient for running on server without X installed. rtorrent has a XML-RPC interface which offers some interesting possibilities for remote control from another application. XML-RPC is a moderately complex protocol and parsing XML is much easier in a high level language. Fortunately someone wrote an XML-RPC utility in Python specifically for rtorrent which makes tinkering a whole easier.

Enabling the XML-RPC interface only requires one line in the .rtorrent.rc config file:
scgi_local = /home/tlow/.rtorrent/socket
You can also enable the interface on a TCP socket but the rtorrent wiki advises against this because of the security implications.

The Python utility can be used on the command line. The following returns the hashes of all the .torrent files currently loaded into into rtorrent:
$ ./xmlrpc2scgi.py scgi:///home/tlow/.rtorrent/socket download_list ''
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param><value><array><data>
<value><string>D19075DB7A5BF5B0B9B89B30EE39CC132BF64F4A</string></value>
<value><string>B00F710FFC9FC09F019FEA2A4718CCFF794475F6</string></value>
<value><string>D7521742B5037E691EFB9CA33C24A29B73F5C68C</string></value>
<value><string>1B74111F0848AA7C056867AEB1A3FBAA744DB356</string></value>
</data></array></value></param>
</params>
</methodResponse>
Obviously you still need to pipe the output to another utility for further processing but the output sort of readable.

The utility can also be used a library module which is much more interesting:
$ ipython
Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import xmlrpc2scgi as xs

In [2]: rtc=xs.RTorrentXMLRPCClient('scgi:///home/tlow/.rtorrent/socket')

In [3]: rtc.download_list('')
Out[3]:
['D19075DB7A5BF5B0B9B89B30EE39CC132BF64F4A',
'B00F710FFC9FC09F019FEA2A4718CCFF794475F6',
'D7521742B5037E691EFB9CA33C24A29B73F5C68C',
'1B74111F0848AA7C056867AEB1A3FBAA744DB356']
Which is the same as the XML output above but as a Python list.

The main problem with rtorrent's XML-RPC interface is that very few of the commands are documented, so it requires some more digging to understand exactly what is possible. I already have an idea of how I want to use the interface, but I'll leave that for another time.

No comments:

Post a Comment