gaffner / PyBitTorrent

MIT License
4 stars 2 forks source link

PyBitTorrent

A Python package for download torrent files.

How to use:

from PyBitTorrent import TorrentClient

client = TorrentClient('~/Downloads/Big Buck Bunny (1920x1080 h.264).torrent')
client.start()

That's it. The TorrentClient accepts the following parameters:

TorrentClient(
    torrent: str,
    max_peers: int,
    use_progress_bar: bool,
    peers_file: str,
    output_dir: str,
)

Simple usage:

You can always use in the Client.py attached in the exampled folder

python examples/Client.py --help

usage: 
    Script for downloading torrent files
     [-h] --torrent TORRENT [--peers PEERS] [--output-directory OUTPUT_DIRECTORY] [--use-progress-bar] [--max-peers MAX_PEERS]

optional arguments:
  -h, --help            show this help message and exit
  --torrent TORRENT     Path of the Torrent file
  --peers PEERS         Path to file contain peers (in the format ip:port for each line)
  --output-directory OUTPUT_DIRECTORY
                        Path to the output directory
  --use-progress-bar    should show progress bar
  --max-peers MAX_PEERS
                        Max connected peers

Example of downloading torrent to "Downloads":

python examples/Client.py --torrent "~/Downloads/Big Buck Bunny (1920x1080 h.264).torrent"--output-directory ~/Downloads
2022-12-24 11:44:48.577 INFO TorrentFile - __init__: Start reading from BitTorrent file
{
    'announce': 'udp://tracker.leechers-paradise.org:6969/announce',
    'announce-list': [
        ['udp://tracker.leechers-paradise.org:6969/announce'],
        ['udp://tracker.internetwarriors.net:1337/announce'],
        ['udp://tracker.opentrackr.org:1337/announce'],
        ...
    ],
    'comment': 'dynamic metainfo from client',
    'created by': 'go.torrent',
    'creation date': 1670818208,
    'info': {'length': 725106140, 'name': 'big_buck_bunny_1080p_h264.mov', 'piece length': 262144, 'pieces': ''}}
}
2022-12-24 11:45:14.507 INFO UDPTracker - get_peers: success in scraping udp://tracker.moeking.me:6969/announce got 88 peers
2022-12-24 11:45:14.923 INFO UDPTracker - get_peers: success in scraping udp://exodus.desync.com:6969/announce got 200 peers
2022-12-24 11:45:25.687 DEBUG PeersManager - send_handshakes: Poll number 1/13
2022-12-24 11:45:25.687 ERROR Bittorrent - handle_messages: No peers found, sleep for 2 seconds
2022-12-24 11:45:25.885 INFO PeersManager - _send_handshake: Trying handshake with peer 92.176.121.23
2022-12-24 11:45:25.984 DEBUG PeersManager - _send_handshake: Adding peer 92.176.121.23, 10982 which is 1/12
2022-12-24 11:45:26.020 INFO PeersManager - _send_handshake: Trying handshake with peer 107.190.125.222
2022-12-24 11:45:26.187 DEBUG Peer - receive_message: Client in ip 107.190.125.222 with id 00000000000000000000 disconnected
2022-12-24 11:45:36.439 DEBUG PeersManager - _send_handshake: Adding peer 189.179.233.150, 35087 which is 2/12
...

Configuration

Non-existing options are ignored.

_documentation: just the link for the documentation, all keys starting with _ are going to be ignored.
listening_port: port to announce to tracker.
max_listening_port: not implemented yet.
max_peers: maximun amount of peers to connect to.
request_interval: not implemented yet.
interation_sleep_interval: interval between each piece request.
logging_level: logging level for Logger.
timeout: timeout for every request made.
max_handshake_threads: Number of handshakes to make in each thread with fixed amount of peers. This value is inversely porportional to the number of threads created.

Experimental options (DO NOT CHANGE)

udp_handshake_threads: bytes to read from UDP tracker requests.
handshake_stripped_size: value to receive from peer requests.
default_connection_id: default connection ID for UDP tracker connections.
compact_value_num_bytes: bytes to read each iteration of compacted peers response.
tcp_only: use only TCP, ignore UDP.

List of BitTorrent messages and their support

Type supported id
Handshake yes -
Keep Alive yes 0
Choke yes 1
Unchoke yes 2
Interested no 3
Not interested no 4
BitField yes 5
Request yes 6
Piece yes 7
Cancel no 8
Port no 9

The architecture of the program


Important notes: