This post polishes up the two previous posts. The idea here was to be able to open a torrent file and make a few modifications. I had a torrent go down on me and hadn't download all the files. I thought I would try removing the tracker and 'private' status and try to connect via DHT. So far this hasn't been successful. I think other peers would need to also enable DHT for their torrents :/ Regardless, it seemed natural to be able to create a file from the python ordered dictionry. The python ordered dictionary is described in this post
from collections import OrderedDict
...
def PyToBEncode(pydict):
bytestringlist = []
def writed(dic):
bytestringlist.append(b'd')
for key in dic:
strlen = len(key)
bytestringlist.append(bytes(str(strlen), encoding='utf8'))
bytestringlist.append(b':')
bytestringlist.append(bytes(key))
value = dic[key]
if type(value) == type(OrderedDict()):
writed(value)
elif type(value) == type([]):
writel(value)
elif type(value) == type(int()):
writei(value)
else:
bytestringlist.append(bytes(str(len(value)), encoding='utf8'))
bytestringlist.append(b':')
bytestringlist.append(bytes(value))
bytestringlist.append(b'e')
def writel(lis):
bytestringlist.append(b'l')
for el in lis:
if type(el) == type(OrderedDict()):
writed(el)
elif type(el) == type([]):
writel(el)
elif type(el) == type(int()):
writei(el)
else:
bytestringlist.append(bytes(str(len(el)), encoding='utf8'))
bytestringlist.append(b':')
bytestringlist.append(bytes(el))
bytestringlist.append(b'e')
def writei(num):
bytestringlist.append(b'i')
bytestringlist.append(bytes(str(num), encoding='utf8'))
bytestringlist.append(b'e')
writed(pydict)
return b''.join(bytestringlist)
Here is a graphic of creating a new torrent. I am still waiting to make a connection.
Showing posts with label bencode. Show all posts
Showing posts with label bencode. Show all posts
Friday, December 6, 2013
Wednesday, December 4, 2013
Converting Bencode to Python Data Structure
This post is motivated by a desire for a custom Bittorrent client. This code will generate a python data structure that mirrors that of the bencoded data using python's ordered dictionary and lists. I have tested this on traditional .torrent files where the bencoded data is a human readable byte string. Other formats, such as some (all?) DHT torrents, are not supported.
from collections import OrderedDict
def BEncodeToPy(bytestring):
'''
#Convertes Bencoded bytestring into python data structure, mirroring data types (doesn't work with magnet torrents)
#Start with the d.
#Next there will be a string that is the dictionaries key
#Then the next entry is one of: dic, list, integer, or string. This is the value for the key
#Handle or call appropriate handler recursively
'''
dex = 0
def OpenList():
nonlocal dex
l = []
dex += 1
while chr(bytestring[dex]) != 'e':
if chr(bytestring[dex]) == 'd':
l.append(OpenDict())
elif chr(bytestring[dex]) == 'l':
l.append(OpenList())
elif chr(bytestring[dex]) == 'i':
dex += 1
l.append(GetInt())
elif chr(bytestring[dex]).isdigit():
strlen = GetStrLen()
l.append(bytestring[dex: dex + strlen])
dex += strlen
dex += 1 #skip over e
return l
def OpenDict():
nonlocal dex
d = OrderedDict()
dex += 1 #GetStrLen should start at beginning of integer
while chr(bytestring[dex]) != 'e':
strlen = GetStrLen()
key = bytestring[dex: dex + strlen]
dex += strlen
if chr(bytestring[dex]) == 'd':
value = OpenDict()
elif chr(bytestring[dex]) == 'l':
value = OpenList()
elif chr(bytestring[dex]) == 'i':
dex += 1 #get int will start with first digit
value = GetInt()
elif chr(bytestring[dex]).isdigit():
strlen = GetStrLen()
value = bytestring[dex: dex + strlen]
dex += strlen
d[key] = value
dex += 1 #skip over e
return d
def GetStrLen():
nonlocal dex
chardex = bytestring.find(b':', dex)
num = int(bytestring[dex:chardex])
dex = chardex + 1
return num
def GetInt():
nonlocal dex
endintdex = bytestring.find(b'e', dex)
num = int(bytestring[dex:endintdex])
dex = endintdex + 1
return num
try:
return OpenDict()
except Exception as e:
print('could not parse')
This graphic compares a binary view of a torrent file with the contents converted to a ordered dictionary in python.
from collections import OrderedDict
def BEncodeToPy(bytestring):
'''
#Convertes Bencoded bytestring into python data structure, mirroring data types (doesn't work with magnet torrents)
#Start with the d.
#Next there will be a string that is the dictionaries key
#Then the next entry is one of: dic, list, integer, or string. This is the value for the key
#Handle or call appropriate handler recursively
'''
dex = 0
def OpenList():
nonlocal dex
l = []
dex += 1
while chr(bytestring[dex]) != 'e':
if chr(bytestring[dex]) == 'd':
l.append(OpenDict())
elif chr(bytestring[dex]) == 'l':
l.append(OpenList())
elif chr(bytestring[dex]) == 'i':
dex += 1
l.append(GetInt())
elif chr(bytestring[dex]).isdigit():
strlen = GetStrLen()
l.append(bytestring[dex: dex + strlen])
dex += strlen
dex += 1 #skip over e
return l
def OpenDict():
nonlocal dex
d = OrderedDict()
dex += 1 #GetStrLen should start at beginning of integer
while chr(bytestring[dex]) != 'e':
strlen = GetStrLen()
key = bytestring[dex: dex + strlen]
dex += strlen
if chr(bytestring[dex]) == 'd':
value = OpenDict()
elif chr(bytestring[dex]) == 'l':
value = OpenList()
elif chr(bytestring[dex]) == 'i':
dex += 1 #get int will start with first digit
value = GetInt()
elif chr(bytestring[dex]).isdigit():
strlen = GetStrLen()
value = bytestring[dex: dex + strlen]
dex += strlen
d[key] = value
dex += 1 #skip over e
return d
def GetStrLen():
nonlocal dex
chardex = bytestring.find(b':', dex)
num = int(bytestring[dex:chardex])
dex = chardex + 1
return num
def GetInt():
nonlocal dex
endintdex = bytestring.find(b'e', dex)
num = int(bytestring[dex:endintdex])
dex = endintdex + 1
return num
try:
return OpenDict()
except Exception as e:
print('could not parse')
This graphic compares a binary view of a torrent file with the contents converted to a ordered dictionary in python.
Subscribe to:
Posts (Atom)

