【问题标题】:How to import bencode cleanly?如何干净地导入bencode?
【发布时间】:2015-05-25 11:11:07
【问题描述】:

我正在尝试使用 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

位于初始化中:

但是使用我的代码 de,由于错误,我无法得到任何结果。确实导入找不到 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

有关 bencode 安装的信息,我刚刚做了“pip install bencode”

【问题讨论】:

    标签: python python-2.7 import importerror bencoding


    【解决方案1】:

    您调用了您的程序bencode.py 这掩盖了已安装的库。重命名脚本并重试:

    更好:

    import bencode
    
    bencode.bdecode(string_to_decode)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      相关资源
      最近更新 更多