added docs for python binding. Fixed warning in python binding.

This commit is contained in:
Arvid Norberg 2007-02-20 04:32:13 +00:00
parent 3fc73cb219
commit df46401c9c
9 changed files with 215 additions and 8 deletions

3
bindings/README.txt Normal file
View File

@ -0,0 +1,3 @@
Documentation covering building and using the python binding for libtorrent
is located in the main doc directory. See docs/python_binding.html

View File

@ -0,0 +1,24 @@
#!/usr/bin/python
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)
h = ses.add_torrent(info, "./", compact_mode = True)
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)

View File

@ -44,9 +44,9 @@ struct entry_to_python
return convert(e.list());
case entry::dictionary_t:
return convert(e.dict());
default:
return object();
}
return object();
}
static PyObject* convert(entry const& e)

View File

@ -37,7 +37,7 @@
<p>Bindings</p>
<ul class="simple">
<li><a class="reference" href="http://libtorrent-ruby.rubyforge.org/">ruby bindings</a></li>
<li><a class="reference" href="http://deluge-torrent.org/trac/wiki/python-libtorrent">python bindings</a></li>
<li><a class="reference" href="python_binding.html">python bindings</a></li>
</ul>
<hr class="docutils" />
<ul class="simple">

View File

@ -63,7 +63,7 @@ libtorrent
.. _sourceforge page: http://www.sourceforge.net/projects/libtorrent
.. _`ruby bindings`: http://libtorrent-ruby.rubyforge.org/
.. _`python bindings`: http://deluge-torrent.org/trac/wiki/python-libtorrent
.. _`python bindings`: python_binding.html
.. _`Introduction, slides`: bittorrent.pdf

94
docs/python_binding.html Normal file
View File

@ -0,0 +1,94 @@
<?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&#64;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>
<td>Arvid Norberg, <a class="last reference" href="mailto:arvid&#64;rasterbar.com">arvid&#64;rasterbar.com</a></td></tr>
</tbody>
</table>
<div class="contents topic" id="table-of-contents">
<p class="topic-title first"><a name="table-of-contents">Table of contents</a></p>
<ul class="simple">
<li><a class="reference" href="#building" id="id1" name="id1">building</a></li>
<li><a class="reference" href="#using" id="id2" name="id2">using</a></li>
</ul>
</div>
<div class="section">
<h1><a id="building" name="building">building</a></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>
<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
<a class="reference" href="building.html#step-2-setup-bbv2">building libtorrent</a> section.</p>
<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
build variants, see <a class="reference" href="building.html#step-3-building-libtorrent">libtorrent build options</a>.</p>
<p>For example:</p>
<pre class="literal-block">
$ bjam dht-support=on 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>
<div class="section">
<h1><a id="using" name="using">using</a></h1>
<p>The python interface is nearly identical to the C++ interface. Please refer to
the <a class="reference" href="manual.html">main library reference</a>.</p>
<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(&quot;test.torrent&quot;, 'rb').read())
info = lt.torrent_info(e)
h = ses.add_torrent(info, &quot;./&quot;, compact_mode = True)
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>

86
docs/python_binding.rst Normal file
View File

@ -0,0 +1,86 @@
=========================
libtorrent python binding
=========================
:Author: Arvid Norberg, arvid@rasterbar.com
.. contents:: Table of contents
:depth: 2
:backlinks: none
building
========
Building the libtorrent python bindings will produce a shared library (DLL)
which is a python module that can be imported in a python program.
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
``$BOOST_BUILD_PATH/user-config.jam``.
Make sure your user config contains the following line::
using python : 2.3 ;
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::
using python : 2.3 : /usr ;
The bindings require *at least* python version 2.2.
For more information on how to install and set up boost-build, see the
`building libtorrent`_ section.
.. _`building libtorrent`: building.html#step-2-setup-bbv2
Once you have boost-build set up, you cd to the ``bindings/python``
directory and invoke ``bjam`` with the apropriate settings. For the available
build variants, see `libtorrent build options`_.
.. _`libtorrent build options`: building.html#step-3-building-libtorrent
For example::
$ bjam dht-support=on release link=static
On Mac OS X, this will produce the following python module::
bin/darwin-4.0/release/dht-support-on/link-static/logging-none/threading-multi/libtorrent.so
using
=====
The python interface is nearly identical to the C++ interface. Please refer to
the `main library reference`_.
.. _`main library reference`: manual.html
For an example python program, see ``client.py`` in the ``bindings/python``
directory.
A very simple example usage of the module would be something like this::
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)
h = ses.add_torrent(info, "./", compact_mode = True)
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)

View File

@ -65,7 +65,7 @@ int main(int argc, char* argv[])
in.unsetf(std::ios_base::skipws);
entry e = bdecode(std::istream_iterator<char>(in), std::istream_iterator<char>());
s.add_torrent(torrent_info(e), "./");
// wait for the user to end
char a;
std::cin.unsetf(std::ios_base::skipws);

View File

@ -60,17 +60,17 @@ void test_transfer()
remove_all("./tmp1");
torrent_handle th = ses.add_torrent(torrent_file, "./tmp1");
for (int i = 0; i < 50; ++i)
for (int i = 0; i < 70; ++i)
{
torrent_status s = th.status();
std::cerr << s.progress << "\r";
std::cerr << s.progress << " " << (s.download_rate / 1000.f) << "\r";
std::auto_ptr<alert> a;
a = ses.pop_alert();
if (a.get())
std::cerr << a->msg() << "\n";
if (th.is_seed()) break;
sleep(100);
sleep(999);
}
TEST_CHECK(th.is_seed());