Skip to main content

Bencode encoder and decoder.

Project description

libbencode

A Python library for Bencode encoding and decoding.

Installation

Installing through pip:

pip install libbencode

Installing through source:

git clone https://github.com/zrekryu/libbencode.git
cd libbencode
pip install .

Usage

Decoding

import libbencode

data: bytes = b'i42e'  # bencode integer.
decoded_data: int = libbencode.decode(data)
print(decoded_data)  # 42

Encoding

import libbencode

data: int = 42  # python integer.
encoded_data: bytes = libbencode.encode(data)
print(encoded_data)  # b'i42e'

Version Information

To print the version of the libbencode library:

import libbencode

print(libbencode.__version__)

Bencode API

Decoding API

All decode_* methods return a tuple of the decoded data and the position of the end byte in the data.

Decoding an Integer

Decodes an integer from bencode format:

from libbencode import Decoder

data: bytes = b"i42e"
decoded_data: tuple[int, int] = Decoder.decode_int(data)
print(decoded_data)  # (42, 3)

Decoding a String

Decodes a string from bencode format:

from libbencode import Decoder

data: bytes = b"4:spam"
decoded_data: tuple[bytes, int] = Decoder.decode_str(data)
print(decoded_data)  # (b'spam', 5)

Decoding with a specific encoding:

decoded_data: tuple[str, int] = Decoder.decode_str(data, encoding="utf-8")  # ('spam', 5)

Decoding a List

Decodes a list from bencode format:

from libbencode import Decoder

data: bytes = b"l4:spami42ee"
decoded_data: tuple[list[bytes | int], int] = Decoder.decode_list(data)
print(decoded_data)  # ([b'spam', 42], 11)

Decoding with a specific encoding:

decoded_data: tuple[list[str | int], int] = Decoder.decode_list(data, encoding="utf-8")  # (['spam', 42], 21)

Decoding a Dictionary

Decodes a dictionary from bencode format:

from libbencode import Decoder

data: bytes = b"d3:bar4:spam3:fooi42ee"
decoded_data: tuple[dict[bytes, bytes | int]] = Decoder.decode_dict(data)
print(decoded_data)  # ({b'bar': b'spam', b'foo': 42}, 21)

Decoding with a specific encoding:

decoded_data: tuple[dict[str, str | int], int] = Decoder.decode_dict(data, encoding="utf-8")  # {'bar': 'spam', 'foo': 42}

Encoding API

Encoding an Integer

from libbencode import Encoder

data: int = 42
encoded_data: bytes = Encoder.encode_int(data)
print(encoded_data)  # b'i42e'

Encoding Bytes

Encodes bytes into bencode format:

from libbencode import Encoder

data: bytes = b"spam"
encoded_data: bytes = Encoder.encode_bytes(data)
print(encoded_data)  # b'4:spam'

Encoding a String

Encodes a string into bencode format:

from libbencode import Encoder

data: str = "spam"
encoded_data: bytes = Encoder.encode_str(data)
print(encoded_data)  # b'4:spam'

Encoding a List

Encodes a list into bencode format:

from libbencode import Encoder

data: list[str | int] = ["spam", 42]
encoded_data: bytes = Encoder.encode_list(data)
print(encoded_data)  # b'l4:spami42ee'

Encoding a Dictionary

Encodes a dictionary into bencode format:

from libbencode import Encoder

data: dict[str, str | int] = {"bar": "spam", "foo": 42}
encoded_data: bytes = Encoder.encode_dict(data)
print(encoded_data)  # b'd3:bar4:spam3:fooi42ee'

Encoding a Boolean

The Bencode format does not natively support boolean values. Therefore, booleans are encoded as integers:

  • True is encoded as b'i1e'
  • False is encoded as b'i0e'

Encodes a boolean into bencode format:

from libbencode import Encoder

data: bool = True
encoded_data: bytes = Encoder.encode_bool(data)
print(encoded_data)  # b'i1e'

Tests

Tests are included in tests directory.

Run tests:

python -m unittest discover -s tests

License

© 2024 Zrekryu. Licensed under MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libbencode-1.0.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

libbencode-1.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file libbencode-1.0.2.tar.gz.

File metadata

  • Download URL: libbencode-1.0.2.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for libbencode-1.0.2.tar.gz
Algorithm Hash digest
SHA256 938db0f98e7b8d4d012ca26e4aff47b9c88a5dc73721966d1f41cf5169adcd67
MD5 20aefabb4ab3c4f7a19d4a716fbc1973
BLAKE2b-256 74af387c54f8e496896868f88b1e41dd13bfc649049489f2900958ebb0d18037

See more details on using hashes here.

File details

Details for the file libbencode-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: libbencode-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for libbencode-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a8183ccf688cda0be636a463d0cbf21a651bb1fb1b44953cbc8191198f553574
MD5 5856d92ffcb34e85267b0c7826c0bd86
BLAKE2b-256 bed87c953dbe38d5f836fc51c04f821626087b0ec9fdfe4c6173d15f0f3d0dff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page