2007-02-20 05:32:13 +01:00
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" xml:lang = "en" lang = "en" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" / >
< meta name = "generator" content = "Docutils 0.5: http://docutils.sourceforge.net/" / >
< title > libtorrent python binding< / title >
< meta name = "author" content = "Arvid Norberg, arvid@rasterbar.com" / >
< link rel = "stylesheet" href = "style.css" type = "text/css" / >
< / head >
< body >
< div class = "document" id = "libtorrent-python-binding" >
< h1 class = "title" > libtorrent python binding< / h1 >
< table class = "docinfo" frame = "void" rules = "none" >
< col class = "docinfo-name" / >
< col class = "docinfo-content" / >
< tbody valign = "top" >
< tr > < th class = "docinfo-name" > Author:< / th >
2008-04-05 06:53:22 +02:00
< td > Arvid Norberg, < a class = "last reference external" href = "mailto:arvid@rasterbar.com" > arvid@ rasterbar.com< / a > < / td > < / tr >
2007-02-20 05:32:13 +01:00
< / tbody >
< / table >
< div class = "contents topic" id = "table-of-contents" >
2008-04-05 06:53:22 +02:00
< p class = "topic-title first" > Table of contents< / p >
2007-02-20 05:32:13 +01:00
< ul class = "simple" >
2008-04-05 06:53:22 +02:00
< li > < a class = "reference internal" href = "#building" id = "id1" > building< / a > < / li >
< li > < a class = "reference internal" href = "#using-libtorrent-in-python" id = "id2" > using libtorrent in python< / a > < / li >
2007-02-20 05:32:13 +01:00
< / ul >
< / div >
2008-04-05 06:53:22 +02:00
< div class = "section" id = "building" >
< h1 > building< / h1 >
2007-02-20 05:32:13 +01:00
< p > Building the libtorrent python bindings will produce a shared library (DLL)
which is a python module that can be imported in a python program.< / p >
< p > The only supported build system for the bindings are currently boost build. To
set up your build environment, you need to add some settings to your
< tt class = "docutils literal" > < span class = "pre" > $BOOST_BUILD_PATH/user-config.jam< / span > < / tt > .< / p >
< p > Make sure your user config contains the following line:< / p >
< pre class = "literal-block" >
using python : 2.3 ;
< / pre >
< p > Set the version to the version of python you have installed or want to use. If
you've installed python in a non-standard location, you have to add the prefix
path used when you installed python as a second option. Like this:< / p >
< pre class = "literal-block" >
using python : 2.3 : /usr ;
< / pre >
< p > The bindings require < em > at least< / em > python version 2.2.< / p >
< p > For more information on how to install and set up boost-build, see the
2008-04-05 06:53:22 +02:00
< a class = "reference external" href = "building.html#step-2-setup-bbv2" > building libtorrent< / a > section.< / p >
2007-02-20 05:32:13 +01:00
< p > Once you have boost-build set up, you cd to the < tt class = "docutils literal" > < span class = "pre" > bindings/python< / span > < / tt >
directory and invoke < tt class = "docutils literal" > < span class = "pre" > bjam< / span > < / tt > with the apropriate settings. For the available
2008-04-05 06:53:22 +02:00
build variants, see < a class = "reference external" href = "building.html#step-3-building-libtorrent" > libtorrent build options< / a > .< / p >
2007-02-20 05:32:13 +01:00
< p > For example:< / p >
< pre class = "literal-block" >
2008-03-10 07:30:34 +01:00
$ bjam dht-support=on boost=source release link=static
2007-02-20 05:32:13 +01:00
< / pre >
< p > On Mac OS X, this will produce the following python module:< / p >
< pre class = "literal-block" >
bin/darwin-4.0/release/dht-support-on/link-static/logging-none/threading-multi/libtorrent.so
< / pre >
< / div >
2008-04-05 06:53:22 +02:00
< div class = "section" id = "using-libtorrent-in-python" >
< h1 > using libtorrent in python< / h1 >
2007-02-20 05:32:13 +01:00
< p > The python interface is nearly identical to the C++ interface. Please refer to
2008-04-05 06:53:22 +02:00
the < a class = "reference external" href = "manual.html" > main library reference< / a > . The main differences are:< / p >
< dl class = "docutils" >
< dt > asio::tcp::endpoint< / dt >
< dd > The endpoint type is represented as a tuple of a string (as the address) and an int for
the port number. E.g. < tt class = "docutils literal" > < span class = "pre" > ('127.0.0.1',< / span > < span class = "pre" > 6881)< / span > < / tt > represents the localhost port 6881.< / dd >
< dt > libtorrent::time_duration< / dt >
< dd > The time duration is represented as a number of seconds in a regular integer.< / dd >
< / dl >
< p > The following functions takes a reference to a container that is filled with
entries by the function. The python equivalent of these functions instead returns
a list of entries.< / p >
< ul class = "simple" >
< li > torrent_handle::get_peer_info< / li >
< li > torrent_handle::file_progress< / li >
< li > torrent_handle::get_download_queue< / li >
< li > torrent_handle::piece_availability< / li >
< / ul >
2007-02-20 05:32:13 +01:00
< p > For an example python program, see < tt class = "docutils literal" > < span class = "pre" > client.py< / span > < / tt > in the < tt class = "docutils literal" > < span class = "pre" > bindings/python< / span > < / tt >
directory.< / p >
< p > A very simple example usage of the module would be something like this:< / p >
< pre class = "literal-block" >
import libtorrent as lt
import time
ses = lt.session()
ses.listen_on(6881, 6891)
e = lt.bdecode(open(" test.torrent" , 'rb').read())
info = lt.torrent_info(e)
2008-03-10 07:30:34 +01:00
h = ses.add_torrent(info, " ./" , storage_mode=storage_mode_sparse)
2007-02-20 05:32:13 +01:00
while (not h.is_seed()):
s = h.status()
state_str = ['queued', 'checking', 'connecting', 'downloading metadata', \
'downloading', 'finished', 'seeding', 'allocating']
print '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
s.num_peers, state_str[s.state])
time.sleep(1)
< / pre >
< / div >
< / div >
< / body >
< / html >