Python Bencoding使用BitTorrent-bencode 5.0.8.1

Bir*_*rla 2 python

我是python和bencoding的新手.我需要使用python为我的项目读取和编写torrent文件.我已经导入了模块,这是我的代码来解析torrent:

这里是我的模块链接http://paste2.org/p/1442120这是一个模http://pypi.python.org/pypi/BitTorrent-bencode/5.0.8.1

            import sys
            from bencode import *
            f = open('file.torrent','rb') #binary read write
            for line in f:
                    print line, 
                    print bdecode(line)
Run Code Online (Sandbox Code Playgroud)

抛出无效的bencoded字符串错误如果我理解正确,bdecode函数一次需要一个值,但我如何解析torrent文件?要么 ...

Die*_*Epp 10

问题是Bencoded文件不是面向行的文件.你正在做的就像拿报告,把它穿过碎纸机,然后一次一把地递给你的老板.以下是解码Bencoded文件的正确方法:

import bencode
print bencode.bdecode(open('file.torrent', 'rb').read())
Run Code Online (Sandbox Code Playgroud)