利用python的BitTorrent扩展库,解析BitTorrent文件

    根据BitTorrent文件的格式,用C++写一个也不难,主要是逐个读和解析,发现python有这库就更好了,下一个BitTorrent的bencod第三方库,其中主要是bencode.py,把它放到工程就行,写了个简单的python就可解析了:


import bencode

def torrent2dict(filename):
    f = open(filename, 'rb')
    s = f.read()
    f.close()
    d = bencode.bdecode(s)
    return d


if __name__ == "__main__":
    fdat = raw_input('please input the torrent file path:')
    print '================================= ', fdat , '========================================='
    d = torrent2dict(fdat)
    for k in d.keys():
        if k == 'info':
            continue            
        print k , ' : ' , d[k]
    
    info = d.get('info')
    for k in info.keys():
        if k == 'pieces':
            print k, ' : ...'
            continue
        print k, ' : ', info[k]



你可能感兴趣的:(Python)