Skip to main content
 首页 » 编程设计

python之如何干净地导入 bencode

2025年05月04日15davidwang456

我正在尝试使用 bencode 库中的 bdecode,也就是说:

def bdecode(x): 
    try: 
        r, l = decode_func[x[0]](x, 0) 
    except (IndexError, KeyError, ValueError): 
        raise BTFailure("not a valid bencoded string") 
    if l != len(x): 
        raise BTFailure("invalid bencoded value (data after valid prefix)") 
    return r 
 
from types import StringType, IntType, LongType, DictType, ListType, TupleType 

位于初始化的此处:

但是由于错误,我的代码无法得到任何结果。确实导入没有找到 bdecode 但我不明白为什么。 这是简单的代码和错误输出:

from bencode import * 
 
blabla = 'd8:announce70:http://tracker.t411.io:56969/c5faa6720249d33ff6ba2af48640af89/announce7:comment29:https://www.t411.io/t/524280210:created by19:https://www.t411.io13:creation datei1431685353e4:infod6:lengthi14634059e4:name22:Charlie-Hebdo-1178.pdf12:piece lengthi262144e6:pieces1120:' 
myprint = bdecode(blabla) 
print myprint 

有关我刚刚制作的“pip install bencode”的 bencode 安装信息

请您参考如下方法:

您将程序命名为 bencode.py 这会屏蔽已安装的库。重命名您的脚本并重试:

更好:

import bencode 
 
bencode.bdecode(string_to_decode)