def encode_dict(data):
	""" Given a dictionary, return the bencoded dictionary. """

	check_type(data, dict)

	# Special case of an empty dictionary.
	if data == {}:
		return "de"

	# Encode each key and value for each key in the dictionary.
	temp = [encode_str(key) + encode(data[key]) for key in sorted(data.keys())]
	# Add dict annotation, and collapse the dictionary.
	return "d" + collapse(temp) + "e"
Esempio n. 2
0
def encode_dict(data):
	""" Given a dictionary, return the bencoded dictionary. """

	check_type(data, dict)

	# Special case of an empty dictionary.
	if data == {}:
		return "de"

	# Encode each key and value for each key in the dictionary.
	temp = [encode_str(key) + encode(data[key]) for key in sorted(data.keys())]
	# Add dict annotation, and collapse the dictionary.
	return "d" + collapse(temp) + "e"
def encode_list(data):
	""" Given a list, returns a bencoded list. """

	check_type(data, list)

	# Special case of an empty list.
	if data == []:
		return "le"

	# Encode each item in the list.
	temp = [encode(item) for item in data]
	# Add list annotation, and collapse the list.
	return "l" + collapse(temp) + "e"
Esempio n. 4
0
def encode_list(data):
	""" Given a list, returns a bencoded list. """

	check_type(data, list)

	# Special case of an empty list.
	if data == []:
		return "le"

	# Encode each item in the list.
	temp = [encode(item) for item in data]
	# Add list annotation, and collapse the list.
	return "l" + collapse(temp) + "e"
def stringlength(string, index = 0):
	""" Given a bencoded expression, starting with a string, returns
	the length of the string. """

	try:
		colon = string.find(":", index)	# Find the colon, ending the number.
	except ValueError:
		raise BencodeError("Decode", "Malformed expression", data)

	# Return a list of the number characters.
	num = [a for a in string[index:colon] if a.isdigit() ]
	n = int(collapse(num))	# Collapse them, and turn them into an int.

	# Return the length of the number, colon, and the string length.
	return len(num) + 1 + n
Esempio n. 6
0
def stringlength(string, index = 0):
	""" Given a bencoded expression, starting with a string, returns
	the length of the string. """

	try:
		colon = string.find(":", index)	# Find the colon, ending the number.
	except ValueError:
		raise BencodeError("Decode", "Malformed expression", data)

	# Return a list of the number characters.
	num = [a for a in string[index:colon] if a.isdigit() ]
	n = int(collapse(num))	# Collapse them, and turn them into an int.

	# Return the length of the number, colon, and the string length.
	return len(num) + 1 + n
Esempio n. 7
0
def info_discionario(file):
	with open(file) as f:
		contents = f.read()
	# varia de acordo com o tamanho do arquivo
	tamanho_pedaco = 524288 
	# objeto info 
	info = {}
	# adicionando informações
	info["tamanho_pedaco"] = tamanho_pedaco
	info["tamanho"] = len(contents)
	info["nome"] = file
	info["md5sum"] = md5(contents).hexdigest()
	# Generate the pieces
	pieces = slice(contents, tamanho_pedaco)
	pieces = [ sha1(p).digest() for p in pieces ]
	info["pieces"] = collapse(pieces)

	return info
Esempio n. 8
0
def make_info_dict(file):
	""" Returns the info dictionary for a torrent file. """

	with open(file) as f:
		contents = f.read()

	piece_length = 524288	# TODO: This should change dependent on file size

	info = {}

	info["piece length"] = piece_length
	info["length"] = len(contents)
	info["name"] = file
	info["md5sum"] = md5(contents).hexdigest()

	# Generate the pieces
	pieces = slice(contents, piece_length)
	pieces = [ sha1(p).digest() for p in pieces ]
	info["pieces"] = collapse(pieces)

	return info
Esempio n. 9
0
def make_info_dict(file):
    """ Returns the info dictionary for a torrent file. """

    with open(file) as f:
        contents = f.read()

    piece_length = 524288  # TODO: This should change dependent on file size

    info = {}

    info["piece length"] = piece_length
    info["length"] = len(contents)
    info["name"] = file
    info["md5sum"] = md5(contents).hexdigest()

    # Generate the pieces
    pieces = slice(contents, piece_length)
    pieces = [sha1(p).digest() for p in pieces]
    info["pieces"] = collapse(pieces)

    return info
Esempio n. 10
0
def make_info_dict(file):
  """ Returns the info dictionary for a torrent file. """

  with open(file) as f:
    contents = f.read()

  piece_length = find_piece_length(len(contents))

  print 'File length "%s", using piece length "%s"' %(len(contents), piece_length,)

  info = {}

  info["piece length"] = piece_length
  info["length"] = len(contents)
  info["name"] = file
  info["md5sum"] = md5(contents).hexdigest()

  # Generate the pieces
  pieces = slice(contents, piece_length)
  pieces = [ sha1(p).digest() for p in pieces ]
  info["pieces"] = collapse(pieces)

  return info
Esempio n. 11
0
	def test_concatenation(self):
		""" Test that characters are correctly concatenated. """

		self.n = util.collapse(["t", "e", "s", "t"])
		self.assertEqual(self.n, "test")
Esempio n. 12
0
    def test_concatenation(self):
        """ Test that characters are correctly concatenated. """

        self.n = util.collapse(["t", "e", "s", "t"])
        self.assertEqual(self.n, "test")