premiere-libtorrent/docs/python_binding.html

208 lines
9.1 KiB
HTML
Raw Normal View History

<?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>
2011-11-19 21:07:41 +01:00
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2014-10-14 18:36:06 +02:00
<meta name="generator" content="Docutils 0.11: http://docutils.sourceforge.net/" />
2011-11-19 21:07:41 +01:00
<title>libtorrent python binding</title>
<meta name="author" content="Arvid Norberg, arvid&#64;libtorrent.org" />
2014-08-26 05:14:32 +02:00
<link rel="stylesheet" type="text/css" href="rst.css" />
<script type="text/javascript">
/* <![CDATA[ */
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
t.parentNode.insertBefore(s, t);
})();
/* ]]> */
</script>
<link rel="stylesheet" href="style.css" type="text/css" />
2008-10-20 00:42:56 +02:00
<style type="text/css">
/* Hides from IE-mac \*/
* html pre { height: 1%; }
/* End hide from IE-mac */
</style>
</head>
<body>
<div class="document" id="libtorrent-python-binding">
2008-10-20 00:42:56 +02:00
<div id="container">
2014-10-14 18:36:06 +02:00
<table id="header">
<tr><td id="orange"></td>
<td id="logo">libtorrent</td></tr>
</table>
2008-10-20 00:42:56 +02:00
<div id="main">
<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>
<td>Arvid Norberg, <a class="last reference external" href="mailto:arvid&#64;libtorrent.org">arvid&#64;libtorrent.org</a></td></tr>
</tbody>
</table>
<div class="contents topic" id="table-of-contents">
<p class="topic-title first">Table of contents</p>
<ul class="simple">
2009-03-21 05:36:46 +01:00
<li><a class="reference internal" href="#building" id="id2">building</a><ul>
<li><a class="reference internal" href="#building-using-setup-py" id="id3">building using setup.py</a></li>
<li><a class="reference internal" href="#building-using-boost-build" id="id4">building using boost build</a></li>
2008-10-20 00:42:56 +02:00
</ul>
</li>
2009-03-21 05:36:46 +01:00
<li><a class="reference internal" href="#using-libtorrent-in-python" id="id5">using libtorrent in python</a></li>
</ul>
</div>
<div class="section" id="building">
<h1>building</h1>
<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>
2008-10-20 00:42:56 +02:00
<div class="section" id="building-using-setup-py">
<h2>building using setup.py</h2>
2013-01-21 20:13:24 +01:00
<p>There is a <tt class="docutils literal">setup.py</tt> shipped with libtorrent that can be used on windows.
On windows the setup.py will invoke <tt class="docutils literal">bjam</tt> and assume that you have boost
sources at <tt class="docutils literal">$BOOST_PATH</tt>. The resulting executable is self-contained, it does
2008-12-02 09:41:02 +01:00
not depend any boost or libtorrent dlls.</p>
<p>On other systems, the setup.py is generated by running
2013-01-21 20:13:24 +01:00
<tt class="docutils literal">./configure <span class="pre">--enable-python-binding</span></tt>.</p>
2008-10-20 00:42:56 +02:00
<p>To build the Python bindings do:</p>
<ol class="arabic">
<li><p class="first">Run:</p>
<pre class="literal-block">
python setup.py build
</pre>
</li>
<li><p class="first">As root, run:</p>
<pre class="literal-block">
python setup.py install
</pre>
</li>
</ol>
</div>
<div class="section" id="building-using-boost-build">
<h2>building using boost build</h2>
<p>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">
2011-11-19 21:07:41 +01:00
using python : 2.6 : /usr/bin/python2.6 : /usr/include/python2.6 : /usr/lib/python2.6 ;
</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
2009-03-21 05:36:46 +01:00
<a class="reference external" href="building.html#step-2-setup-bbv2">building libtorrent</a> section.</p>
2013-01-21 20:13:24 +01:00
<p>Once you have boost-build set up, you cd to the <tt class="docutils literal">bindings/python</tt>
directory and invoke <tt class="docutils literal">bjam</tt> with the apropriate settings. For the available
2009-03-21 05:36:46 +01:00
build variants, see <a class="reference external" href="building.html#step-3-building-libtorrent">libtorrent build options</a>.</p>
<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
</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-10-20 00:42:56 +02:00
</div>
<div class="section" id="using-libtorrent-in-python">
<h1>using libtorrent in python</h1>
<p>The python interface is nearly identical to the C++ interface. Please refer to
2013-08-12 20:32:41 +02:00
the <a class="reference external" href="reference.html">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
2013-01-21 20:13:24 +01:00
the port number. E.g. <tt class="docutils literal">('127.0.0.1', 6881)</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>
<p><tt class="docutils literal"><span class="pre">create_torrent::add_node()</span></tt> takes two arguments, one string and one integer,
instead of a pair. The string is the address and the integer is the port.</p>
2013-01-21 20:13:24 +01:00
<p><tt class="docutils literal"><span class="pre">session::set_settings()</span></tt> not only accepts a <tt class="docutils literal">session_settings</tt> object, but also
a dictionary with keys matching the names of the members of the <tt class="docutils literal">session_settings</tt> struct.
When calling <tt class="docutils literal">set_settings</tt>, the dictionary does not need to have every settings set,
2011-01-18 04:41:54 +01:00
keys that are not present, are set to their default value.</p>
2013-01-21 20:13:24 +01:00
<p>For backwards compatibility, <tt class="docutils literal"><span class="pre">session::settings()</span></tt> still returns a <tt class="docutils literal">session_settings</tt>
2011-01-18 04:41:54 +01:00
struct. To get a python dictionary of the settings, call <tt class="docutils literal"><span class="pre">session::get_settings</span></tt>.</p>
2013-01-21 20:13:24 +01:00
<p>For an example python program, see <tt class="docutils literal">client.py</tt> in the <tt class="docutils literal">bindings/python</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(&quot;test.torrent&quot;, 'rb').read())
info = lt.torrent_info(e)
params = { save_path: './', \
storage_mode: lt.storage_mode_t.storage_mode_sparse, \
ti: info }
h = ses.add_torrent(params)
while (not h.is_seed()):
s = h.status()
2008-10-21 11:31:16 +02:00
state_str = ['queued', 'checking', '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>
2014-10-14 18:36:06 +02:00
</div>
</div>
<div id="gradient"></div>
<div id="footer">
<table>
<tr>
<td><a href="index.html">home</a></td>
<td><a href="http://blog.libtorrent.org">blog</a></td>
<td><a href="utp.html">uTP</a></td>
</tr>
<tr>
<td><a href="https://sourceforge.net/projects/libtorrent/files/libtorrent/">download</a></td>
<td><a href="reference.html">documentation</a></td>
<td><a href="dht_store.html">DHT put extension</a></td>
</tr>
<tr>
<td><a href="https://sourceforge.net/projects/libtorrent/files/py-libtorrent/">python bindings</a></td>
<td><a href="features.html">features</a></td>
<td><a href="dht_sec.html">DHT security extension</a></td>
</tr>
<tr>
<td><a href="http://dir.gmane.org/gmane.network.bit-torrent.libtorrent">mailing list archive</a></td>
<td><a href="contributing.html">contributing</a></td>
<td><a href="streaming.html">streaming</a></td>
</tr>
<tr>
<td><a href="http://code.google.com/p/libtorrent/issues/entry">report a bug</a></td>
<td><a href="building.html">building</a></td>
<td><a href="bittorrent.pdf">bittorrent slides</a></td>
</tr>
</table>
</div>
<div id="filler"></div></div>
</div>
</body>
</html>