libbencode


Namelibbencode JSON
Version 1.0.2 PyPI version JSON
download
home_pageNone
SummaryBencode encoder and decoder.
upload_time2024-09-21 05:03:43
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseNone
keywords bencode bittorrent torrent
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # libbencode

A Python library for Bencode encoding and decoding.

# Installation

Installing through pip:
```bash
pip install libbencode
```

Installing through source:
```bash
git clone https://github.com/zrekryu/libbencode.git
cd libbencode
pip install .
```

# Usage

## Decoding
```py
import libbencode

data: bytes = b'i42e'  # bencode integer.
decoded_data: int = libbencode.decode(data)
print(decoded_data)  # 42
```
## Encoding
```py
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:
```py
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:
```py
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:
```py
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:
```py
decoded_data: tuple[str, int] = Decoder.decode_str(data, encoding="utf-8")  # ('spam', 5)
```

### Decoding a List

Decodes a list from bencode format:
```py
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:
```py
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:
```py
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:
```py
decoded_data: tuple[dict[str, str | int], int] = Decoder.decode_dict(data, encoding="utf-8")  # {'bar': 'spam', 'foo': 42}
```

## Encoding API

### Encoding an Integer
```py
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:
```py
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:
```py
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:
```py
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:
```py
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:
```py
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:
```bash
python -m unittest discover -s tests
```

# License

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

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "libbencode",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "bencode, bittorrent, torrent",
    "author": null,
    "author_email": "Zrekryu <zrekryu@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/74/af/387c54f8e496896868f88b1e41dd13bfc649049489f2900958ebb0d18037/libbencode-1.0.2.tar.gz",
    "platform": null,
    "description": "# libbencode\n\nA Python library for Bencode encoding and decoding.\n\n# Installation\n\nInstalling through pip:\n```bash\npip install libbencode\n```\n\nInstalling through source:\n```bash\ngit clone https://github.com/zrekryu/libbencode.git\ncd libbencode\npip install .\n```\n\n# Usage\n\n## Decoding\n```py\nimport libbencode\n\ndata: bytes = b'i42e'  # bencode integer.\ndecoded_data: int = libbencode.decode(data)\nprint(decoded_data)  # 42\n```\n## Encoding\n```py\nimport libbencode\n\ndata: int = 42  # python integer.\nencoded_data: bytes = libbencode.encode(data)\nprint(encoded_data)  # b'i42e'\n```\n\n## Version Information\n\nTo print the version of the `libbencode` library:\n```py\nimport libbencode\n\nprint(libbencode.__version__)\n```\n\n# Bencode API\n\n## Decoding API\n\nAll `decode_*` methods return a tuple of the decoded data and the position of the end byte in the data.\n\n### Decoding an Integer\n\nDecodes an integer from bencode format:\n```py\nfrom libbencode import Decoder\n\ndata: bytes = b\"i42e\"\ndecoded_data: tuple[int, int] = Decoder.decode_int(data)\nprint(decoded_data)  # (42, 3)\n```\n\n### Decoding a String\n\nDecodes a string from bencode format:\n```py\nfrom libbencode import Decoder\n\ndata: bytes = b\"4:spam\"\ndecoded_data: tuple[bytes, int] = Decoder.decode_str(data)\nprint(decoded_data)  # (b'spam', 5)\n```\n\nDecoding with a specific encoding:\n```py\ndecoded_data: tuple[str, int] = Decoder.decode_str(data, encoding=\"utf-8\")  # ('spam', 5)\n```\n\n### Decoding a List\n\nDecodes a list from bencode format:\n```py\nfrom libbencode import Decoder\n\ndata: bytes = b\"l4:spami42ee\"\ndecoded_data: tuple[list[bytes | int], int] = Decoder.decode_list(data)\nprint(decoded_data)  # ([b'spam', 42], 11)\n```\n\nDecoding with a specific encoding:\n```py\ndecoded_data: tuple[list[str | int], int] = Decoder.decode_list(data, encoding=\"utf-8\")  # (['spam', 42], 21)\n```\n\n### Decoding a Dictionary\n\nDecodes a dictionary from bencode format:\n```py\nfrom libbencode import Decoder\n\ndata: bytes = b\"d3:bar4:spam3:fooi42ee\"\ndecoded_data: tuple[dict[bytes, bytes | int]] = Decoder.decode_dict(data)\nprint(decoded_data)  # ({b'bar': b'spam', b'foo': 42}, 21)\n```\n\nDecoding with a specific encoding:\n```py\ndecoded_data: tuple[dict[str, str | int], int] = Decoder.decode_dict(data, encoding=\"utf-8\")  # {'bar': 'spam', 'foo': 42}\n```\n\n## Encoding API\n\n### Encoding an Integer\n```py\nfrom libbencode import Encoder\n\ndata: int = 42\nencoded_data: bytes = Encoder.encode_int(data)\nprint(encoded_data)  # b'i42e'\n```\n\n### Encoding Bytes\n\nEncodes bytes into bencode format:\n```py\nfrom libbencode import Encoder\n\ndata: bytes = b\"spam\"\nencoded_data: bytes = Encoder.encode_bytes(data)\nprint(encoded_data)  # b'4:spam'\n```\n\n### Encoding a String\n\nEncodes a string into bencode format:\n```py\nfrom libbencode import Encoder\n\ndata: str = \"spam\"\nencoded_data: bytes = Encoder.encode_str(data)\nprint(encoded_data)  # b'4:spam'\n```\n\n### Encoding a List\n\nEncodes a list into bencode format:\n```py\nfrom libbencode import Encoder\n\ndata: list[str | int] = [\"spam\", 42]\nencoded_data: bytes = Encoder.encode_list(data)\nprint(encoded_data)  # b'l4:spami42ee'\n```\n\n### Encoding a Dictionary\n\nEncodes a dictionary into bencode format:\n```py\nfrom libbencode import Encoder\n\ndata: dict[str, str | int] = {\"bar\": \"spam\", \"foo\": 42}\nencoded_data: bytes = Encoder.encode_dict(data)\nprint(encoded_data)  # b'd3:bar4:spam3:fooi42ee'\n```\n\n### Encoding a Boolean\n\nThe Bencode format does not natively support boolean values. Therefore, booleans are encoded as integers:\n\n- True is encoded as `b'i1e'`\n- False is encoded as `b'i0e'`\n\nEncodes a boolean into bencode format:\n```py\nfrom libbencode import Encoder\n\ndata: bool = True\nencoded_data: bytes = Encoder.encode_bool(data)\nprint(encoded_data)  # b'i1e'\n```\n\n# Tests\n\nTests are included in `tests` directory.\n\nRun tests:\n```bash\npython -m unittest discover -s tests\n```\n\n# License\n\n\u00a9 2024 Zrekryu. Licensed under MIT License. See the LICENSE file for details.",
    "bugtrack_url": null,
    "license": null,
    "summary": "Bencode encoder and decoder.",
    "version": "1.0.2",
    "project_urls": {
        "Issues": "https://github.com/zrekryu/libbencode/issues",
        "Repository": "https://github.com/zrekryu/libbencode"
    },
    "split_keywords": [
        "bencode",
        " bittorrent",
        " torrent"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bed87c953dbe38d5f836fc51c04f821626087b0ec9fdfe4c6173d15f0f3d0dff",
                "md5": "5856d92ffcb34e85267b0c7826c0bd86",
                "sha256": "a8183ccf688cda0be636a463d0cbf21a651bb1fb1b44953cbc8191198f553574"
            },
            "downloads": -1,
            "filename": "libbencode-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5856d92ffcb34e85267b0c7826c0bd86",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 5904,
            "upload_time": "2024-09-21T05:03:41",
            "upload_time_iso_8601": "2024-09-21T05:03:41.935907Z",
            "url": "https://files.pythonhosted.org/packages/be/d8/7c953dbe38d5f836fc51c04f821626087b0ec9fdfe4c6173d15f0f3d0dff/libbencode-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "74af387c54f8e496896868f88b1e41dd13bfc649049489f2900958ebb0d18037",
                "md5": "20aefabb4ab3c4f7a19d4a716fbc1973",
                "sha256": "938db0f98e7b8d4d012ca26e4aff47b9c88a5dc73721966d1f41cf5169adcd67"
            },
            "downloads": -1,
            "filename": "libbencode-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "20aefabb4ab3c4f7a19d4a716fbc1973",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 5505,
            "upload_time": "2024-09-21T05:03:43",
            "upload_time_iso_8601": "2024-09-21T05:03:43.196690Z",
            "url": "https://files.pythonhosted.org/packages/74/af/387c54f8e496896868f88b1e41dd13bfc649049489f2900958ebb0d18037/libbencode-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-21 05:03:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zrekryu",
    "github_project": "libbencode",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "libbencode"
}
        
Elapsed time: 0.39970s