使用BitTorrentbencode 5.0.8.1的Python Bencoding

2024-06-02 11:29:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

这将引发无效的bencoded字符串错误 如果我理解正确,bdecode函数一次需要一个值,但是如何解析torrent文件?或者。。。在


Tags: 模块文件项目代码orgimportpypihttp
1条回答
网友
1楼 · 发布于 2024-06-02 11:29:41

问题是Bencoded文件不是面向行的文件。你所做的就像做一份报告,把它放到碎纸机里,然后一份一份地交给你的老板。以下是解码Bencoded文件的正确方法:

import bencode
print bencode.bdecode(open('file.torrent', 'rb').read())

相关问题 更多 >