def test_dict(self): assert bencode({}) == 'de' assert bencode({ 'age': 25, 'eyes': 'blue' }) == 'd3:agei25e4:eyes4:bluee' assert bencode({'spam.mp3': { 'author': 'Alice', 'length': 100000 }}) == 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee'
def rest_announce(): try: log.debug("Enter announce") ACTIVE_INTERVAL = current_app.config['ACTIVE_INTERVAL'] PASSIVE_INTERVAL = current_app.config['PASSIVE_INTERVAL'] ihash = utils.get_hash_hex(request.args.get('info_hash')) log.debug("ihash: %s" % ihash) if request.args.get('ip'): ipaddress = request.args.get('ip') else: ipaddress = request.remote_addr port = request.args.get('port') thispeer = "%s:%s" % (ipaddress, port) log.debug("peerpair: %s" % thispeer) seeder = int(request.args.get('left')) == 0 active = utils.is_active(ihash) if active: interval = ACTIVE_INTERVAL if peer_first_announce(ihash, thispeer): store_peer_data(request.args, ihash, thispeer) else: update_peer_data(request.args, ihash, thispeer) warning = "" else: interval = PASSIVE_INTERVAL warning = "No active transfer for that info_hash: %s" % ihash peers = get_peers_for_peer(ihash, thispeer, seeder, active) if thispeer in peers: peers.remove(thispeer) log.debug("Will send thispeer: %s these peers: %s" % (thispeer, peers)) if request.args.get('compact') == '1': peerlist = "" for peer in peers: peerlist += convert_peerpair_to_pack(peer) else: peerlist = [] for peer in peers: peerlist.append(convert_peerpair_to_rec(ihash, peer)) response = { "interval": interval, "warning": warning, "peers": peerlist } log.debug("resp: %s " % response) return bencode.bencode(response) except Exception, e: log.critical("Caught unhandled exception in announce") log.exception(e) raise e
def rest_announce(): try: log.debug("Enter announce") ACTIVE_INTERVAL = current_app.config["ACTIVE_INTERVAL"] PASSIVE_INTERVAL = current_app.config["PASSIVE_INTERVAL"] ihash = utils.get_hash_hex(request.args.get("info_hash")) log.debug("ihash: %s" % ihash) if request.args.get("ip"): ipaddress = request.args.get("ip") else: ipaddress = request.remote_addr port = request.args.get("port") thispeer = "%s:%s" % (ipaddress, port) log.debug("peerpair: %s" % thispeer) seeder = int(request.args.get("left")) == 0 active = utils.is_active(ihash) if active: interval = ACTIVE_INTERVAL if peer_first_announce(ihash, thispeer): store_peer_data(request.args, ihash, thispeer) else: update_peer_data(request.args, ihash, thispeer) warning = "" else: interval = PASSIVE_INTERVAL warning = "No active transfer for that info_hash: %s" % ihash peers = get_peers_for_peer(ihash, thispeer, seeder, active) if thispeer in peers: peers.remove(thispeer) log.debug("Will send thispeer: %s these peers: %s" % (thispeer, peers)) if request.args.get("compact") == "1": peerlist = "" for peer in peers: peerlist += convert_peerpair_to_pack(peer) else: peerlist = [] for peer in peers: peerlist.append(convert_peerpair_to_rec(ihash, peer)) response = {"interval": interval, "warning": warning, "peers": peerlist} log.debug("resp: %s " % response) return bencode.bencode(response) except Exception, e: log.critical("Caught unhandled exception in announce") log.exception(e) raise e
def get_infohash_from_torrent(data): """Return the sha1 hash for the torrent""" return hashlib.sha1(bencode.bencode(data['info'])).hexdigest()
def get_infohash_from_file(file): '''Return a string hash value for a torrentfile, raise exception otherwise''' file.seek(0) data = bencode.bdecode(file.read()) return hashlib.sha1(bencode.bencode(data['info'])).hexdigest()
def get_infohash(data): """Return the sha1 hash for the torrent""" return hashlib.sha1(bencode.bencode(data["info"])).hexdigest()
def test_string(self): assert bencode('') == '0:' assert bencode('abc') == '3:abc' assert bencode('1234567890') == '10:1234567890'
def test_fail_integerkeyed_dict(self): try: bencode({1: 'foo'}) assert False except AssertionError: pass
def test_list(self): assert bencode([]) == 'le' assert bencode([1, 2, 3]) == 'li1ei2ei3ee' assert bencode([['Alice', 'Bob'], [2, 3]]) == 'll5:Alice3:Bobeli2ei3eee'
def test_integer(self): assert bencode(4) == 'i4e' assert bencode(0) == 'i0e' assert bencode(-10) == 'i-10e' assert bencode(12345678901234567890L) == 'i12345678901234567890e'
def test_dict(self): assert bencode({}) == 'de' assert bencode({'age': 25, 'eyes': 'blue'}) == 'd3:agei25e4:eyes4:bluee' assert bencode({'spam.mp3': {'author': 'Alice', 'length': 100000}}) == 'd8:spam.mp3d6:author5:Alice6:lengthi100000eee'