diff --git a/README.md b/README.md index 5d02f27..6a7b20c 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ a single component. - krpc.py - implements the basic UDP Kademila-RPC protocol layer - dht.py - contains the code for accessing the Mainline DHT using KRPC - + - tracker.py - implements the UDP and HTTP tracker protocol for peer discovery KRPC Implementation ------------------- @@ -71,3 +71,21 @@ and allow access to the discovered external connection infos: Start shutdown of the local DHT peer and all associated maintainance threads. - get_external_connection() Return the discovered external connection infos + + +Tracker Implementation +---------------------- + +The tracker implementation provides functions to get the list of peers for a certain info_hash from +a HTTP or UDP tracker. Each function returns a list of connection tuples with possible peers for +the given info hash. The three non-optional parameters are the tracker url, info_hash and peer_id. +Optionally it is possible to provide the peer ip and peer port, the amount already uploaded and downloaded +as well as the amount left to download and the occasion / event of the request. This event can be +either 'started', 'stopped', 'completed' or 'empty' (for regular queries). + + - http_get_peers(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, + uploaded = 0, downloaded = 0, left = 0, event = 'started') + - udp_get_peers(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, + uploaded = 0, downloaded = 0, left = 0, event = 'started', num_want = -1, key = 0) + With num_want it is possible to tell the tracker who many peers should be sent. The parameter key + should be a unique key that is randomized by the client. diff --git a/tracker.py b/tracker.py index ccefacd..74afb65 100644 --- a/tracker.py +++ b/tracker.py @@ -51,9 +51,9 @@ def decode_connections(data): data = data[6:] # Implementation of BEP #0015 (UDP tracker protocol) -def udp_announce(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, event = None, - num_want = -1, downloaded = 0, uploaded = 0, left = 0, key = 0): - event = {None: 0, 'completed': 1, 'started': 2, 'stopped': 3}[event] +def udp_get_peers(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, + uploaded = 0, downloaded = 0, left = 0, event = 'started', num_want = -1, key = 0): + event = {'empty': 0, 'completed': 1, 'started': 2, 'stopped': 3}[event] url = parse_url(tracker_url) conn = (socket.gethostbyname(url.hostname), url.port) sock = UDPSocket(('0.0.0.0', 0)) @@ -117,8 +117,8 @@ def udp_announce(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, even sock.close() # Implementation of BEP #0003 (Bittorrent - section: HTTP Tracker protocol) -def http_announce(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, event = 'started', - uploaded = 0, downloaded = 0, left = 0): +def http_get_peers(tracker_url, info_hash, peer_id, ip = '0.0.0.0', port = 0, + uploaded = 0, downloaded = 0, left = 0, event = 'started'): query = {b'info_hash': info_hash, b'peer_id': peer_id, b'ip': ip, b'port': port, b'uploaded': uploaded, b'downloaded': downloaded, b'left': left, b'compact': 1} if event: @@ -132,5 +132,5 @@ if __name__ == '__main__': import os, binascii peer_id = os.urandom(20) info_hash = binascii.unhexlify('ae3fa25614b753118931373f8feae64f3c75f5cd') # Ubuntu 15.10 info hash - print(http_announce('http://torrent.ubuntu.com:6969/announce', info_hash, peer_id)) - print(udp_announce('udp://tracker.openbittorrent.com:80', info_hash, peer_id)) + print(http_get_peers('http://torrent.ubuntu.com:6969/announce', info_hash, peer_id)) + print(udp_get_peers('udp://tracker.openbittorrent.com:80', info_hash, peer_id))