Search code examples
pythonpython-2.7importimporterrorbencoding

How to import bencode cleanly?


I'm trying to use bdecode from the bencode library that is to say this:

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

Located here in the init :

enter image description here

But with my code de i can't have any result because of an error. Indeed the import dont find bdecode but i don't understand why. This is the simple code and the error output:

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

enter image description here

For information on the bencode installation i just made "pip install bencode"


Solution

  • You called your program bencode.py this masks the installed library. Rename your script and try again:

    Better:

    import bencode
    
    bencode.bdecode(string_to_decode)