def decode(self, bencoded_message): """ Decodes a bencoded message :param bencoded_message: the bencoded message :return: the original message """ bc = bencodepy.Bencode(encoding='utf-8') return bc.decode(bencoded_message)
def loads(self, s: str, options): kwargs = {} kwargs.update(Options.pop_origin_kwargs(options)) self.check_options(options) bc = bencodepy.Bencode(encoding='utf-8') try: return bc.decode(s, **kwargs) except bencodepy.BencodeDecodeError as e: raise SerializeError(e)
def _decode(self, txt) -> dict: """ Utility function that decodes bencoded text into ordered dict. """ bc = bencodepy.Bencode( encoding=None, encoding_fallback=None, dict_ordered=True, dict_ordered_sort=True) return bc.decode(txt)
def __init__(self, file_path): bc = bencodepy.Bencode(encoding=None, encoding_fallback=None, dict_ordered=True, dict_ordered_sort=True) self.decoded_info = bc.read(file_path) #hash the info dict for the tracker info = bc.encode(self.decoded_info[b'info']) self.info_hash = sha1(info).digest() #generate list of files self._generate_file_list()
def from_torrent(fpath): bencoder = bencodepy.Bencode() bdencoded_torrent = bencoder.read(fpath) announce_url = bdencoded_torrent[b"announce"].decode() single_file = bdencoded_torrent[b"info"].get(b"name") file_length = bdencoded_torrent[b"info"].get(b"length") multi_files = bdencoded_torrent[b"info"].get(b"files") pieces_length = bdencoded_torrent[b"info"][b"piece length"] pieces_hash = bdencoded_torrent[b"info"][b"pieces"] pieces_amount = -(-len(pieces_hash) // 20) bencoded_info = bencodepy.encode(bdencoded_torrent[b"info"]) if single_file and not multi_files: return ( announce_url, single_file.decode(), file_length, None, pieces_length, pieces_amount, pieces_hash, bencoded_info, ) elif multi_files: return ( announce_url, single_file, None, multi_files, pieces_length, pieces_amount, pieces_hash, bencoded_info, )
def decode(self, bencoded_message): # decodes a bencode message bc = bencodepy.Bencode(encoding='utf-8') return bc.decode(bencoded_message)
import threading import socketserver import logging import bencodepy import time import random import string from utils import * from sockets import UDPSocket bc = bencodepy.Bencode(encoding='utf-8') rtpe_socket = None envoy_socket = None config = {} class ThreadedUDPRequestHandler(socketserver.BaseRequestHandler): def handle(self): global rtpe_socket global envoy_socket rtpe_address = (config['rtpe_address'], int(config["rtpe_port"])) envoy_address = (config['envoy_address'], int(config['envoy_port'])) raw_data = self.request[0].strip() socket = self.request[1] data = parse_data(raw_data) logging.info(f'Received {data["command"]}') logging.debug(f'Received message: {raw_data}')
def get_infohash(): # Tambien se consigue el hash del valor del campo info encontrado en archivo torrent bencoder = bencodepy.Bencode() bdencoded_torrent = bencoder.read(TORRENT_FILE) bencoded_info = bencodepy.encode(bdencoded_torrent[b"info"]) return hashlib.sha1(bencoded_info).digest()
default="./", help="Destination for Downloaded File", metavar='\b') parser.add_argument( "-w", "--write-method", dest="method", default=1, type=int, help= "1 -- Write File as Pieces Arrive\n2 -- Write File After all the Pieces Arrive", metavar='\b') args = parser.parse_args() bc = bencodepy.Bencode(encoding=None) with open(args.torrent_path, 'rb') as f: torrent = bencodepy.bread(f) client = client_node.Client(torrent, args) peer_list = tracker.getPeers(torrent) print("Peer List:\n", peer_list) for item in peer_list: peer = peer_node.Peer(item, torrent, args) peer.download(client) if client.is_done(): break