2007-02-20 05:32:13 +01:00
=========================
libtorrent python binding
=========================
2018-10-01 11:34:06 +02:00
.. include :: header.rst
2007-02-20 05:32:13 +01:00
.. 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.
2018-03-06 08:53:39 +01:00
building using boost build (windows)
------------------------------------
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
Download and install `Visual C++ 2015 Build Tools`__
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
.. __: http://landinghub.visualstudio.com/visual-cpp-build-tools
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
Download `Boost libraries`__ Extract it to c:/Libraries/boost_1_63_0 and create these environmental vars:
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
.. __: http://www.boost.org/users/history/
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
1. BOOST_BUILD_PATH: "c:/Libraries/boost_1_63_0/tools/build/"
2. BOOST_ROOT: "c:/Libraries/boost_1_63_0/"
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
Navigate to BOOST_ROOT, execute "bootstrap.bat" and add to the path "c:/Libraries/boost_1_63_0/tools/build/src/engine/bin.ntx86/"
Move the file `` user-config.jam `` from `` %BOOST_BUILD_PATH%/example/ `` to `` %BOOST_BUILD_PATH%/user-config.jam `` and add this at the end:
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
::
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
using msvc : 14.0 : : /std:c++11 ;
using python : 3.5 : C:/Users/<UserName>/AppData/Local/Programs/Python/Python35 : C:/Users/<UserName>/AppData/Local/Programs/Python/Python35/include : C:/Users/<UserName>/AppData/Local/Programs/Python/Python35/libs ;
2008-10-17 09:42:17 +02:00
2018-03-06 08:53:39 +01:00
(change the python path for yours)
2008-10-17 09:42:17 +02:00
2018-03-25 08:36:48 +02:00
Navigate to bindings/python and execute::
2018-03-06 08:53:39 +01:00
python setup.py build --bjam
2018-10-19 19:56:07 +02:00
Note: If you are using 64 bits python you should edit setup.py and add this to the b2 command:
2018-03-06 08:53:39 +01:00
`` address-model=64 ``
This will create the file libtorrent.pyd inside build/lib/ that contains the binding.
building using boost build (others)
-----------------------------------
2008-10-17 09:42:17 +02:00
To set up your build environment, you need to add some settings to your
2007-02-20 05:32:13 +01:00
`` $BOOST_BUILD_PATH/user-config.jam `` .
2018-03-06 08:53:39 +01:00
A similar line to this line should be in the file (could be another python version)::
2007-02-20 05:32:13 +01:00
2018-03-06 08:53:39 +01:00
#using python : 2.3 ;
2007-02-20 05:32:13 +01:00
2018-03-06 08:53:39 +01:00
Uncomment it and change it with the version of python you have installed or want to use. If
2007-02-20 05:32:13 +01:00
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::
2011-11-13 07:22:08 +01:00
using python : 2.6 : /usr/bin/python2.6 : /usr/include/python2.6 : /usr/lib/python2.6 ;
2007-02-20 05:32:13 +01:00
The bindings require *at least* python version 2.2.
For more information on how to install and set up boost-build, see the
2008-10-17 19:31:20 +02:00
`building libtorrent`__ section.
2007-02-20 05:32:13 +01:00
2008-10-17 19:31:20 +02:00
.. __: building.html#step-2-setup-bbv2
2007-02-20 05:32:13 +01:00
Once you have boost-build set up, you cd to the `` bindings/python ``
2018-10-19 19:56:07 +02:00
directory and invoke `` bjam `` with the appropriate settings. For the available
2007-02-20 05:32:13 +01:00
build variants, see `libtorrent build options`_ .
.. _`libtorrent build options`: building.html#step-3-building-libtorrent
For example::
2015-08-09 01:18:05 +02:00
$ bjam dht-support=on link=static
2007-02-20 05:32:13 +01:00
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
2008-03-28 22:37:35 +01:00
using libtorrent in python
==========================
2007-02-20 05:32:13 +01:00
The python interface is nearly identical to the C++ interface. Please refer to
2013-08-12 05:18:43 +02:00
the `library reference`_ . The main differences are:
2008-03-28 22:37:35 +01:00
asio::tcp::endpoint
The endpoint type is represented as a tuple of a string (as the address) and an int for
2018-10-19 19:56:07 +02:00
the port number. E.g. `` ("127.0.0.1", 6881) `` represents the localhost port 6881.
2008-03-28 22:37:35 +01:00
2017-04-12 20:05:53 +02:00
lt::time_duration
2008-03-28 22:37:35 +01:00
The time duration is represented as a number of seconds in a regular integer.
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.
* torrent_handle::get_peer_info
* torrent_handle::file_progress
* torrent_handle::get_download_queue
* torrent_handle::piece_availability
2010-05-22 10:21:40 +02:00
`` create_torrent::add_node() `` takes two arguments, one string and one integer,
instead of a pair. The string is the address and the integer is the port.
2007-02-20 05:32:13 +01:00
2016-03-18 18:43:11 +01:00
`` session::apply_settings() `` accepts a dictionary with keys matching the names
of settings in settings_pack.
When calling `` apply_settings `` , the dictionary does not need to have every settings set,
keys that are not present are not updated.
2010-12-26 09:03:02 +01:00
2016-03-18 18:43:11 +01:00
To get a python dictionary of the settings, call `` session::get_settings `` .
2010-12-26 09:03:02 +01:00
2013-08-12 05:18:43 +02:00
.. _`library reference`: reference.html
2007-02-20 05:32:13 +01:00
2017-05-01 18:05:24 +02:00
Retrieving session statistics in Python is more convenient than that in C++.
The statistics are stored as an array in `` session_stats_alert `` , which will be posted after calling `` post_session_stats() `` in the `` session `` object.
In order to interpret the statistics array, in C++ it is required to call `` session_stats_metrics() `` to get the indices of these metrics, while in Python it can be done using `` session_stats_alert.values["NAME_OF_METRIC"] `` , where `` NAME_OF_METRIC `` is the name of a metric.
2007-02-20 05:32:13 +01:00
For an example python program, see `` client.py `` in the `` bindings/python ``
directory.
2017-11-23 20:00:58 +01:00
A very simple example usage of the module would be something like this:
2007-02-20 05:32:13 +01:00
2017-11-23 20:00:58 +01:00
.. include :: ../bindings/python/simple_client.py
:code: python
:tab-width: 2
:start-after: from __future__ import print_function
2007-02-20 05:32:13 +01:00