Skip to main content

Basic Socket Protocol

Project description

Basic Socket Protocol

This is a Python3 version of the original BSP protocol distributed in JavaScript, for more detail of the protocol and supported data types, view it on GitHub: bsp.

The Node.js BSP was introduced, in the first place, to solve the problem that when transporting data over sockets in Node.js, the under-hood algorithm will automatically split the data into segments or stick multiple data into one piece, trying to optimize the network usage. Which however causes many pains and mistakes when transferring the data.

The purpose of BSP is two encode the data, and no matter how it may be sticked or splitted during the transmission, the remote end can still be able to decode it as-is when receiving it or them in a stream. And more over, allow the transport of arbitrary data, instead of only string or binary.

The Python version of BSP, is meant to allow transmitting data between Python and Node.js/Browser, and any other environments that has BSP implemented.

API

# Encodes any number of data into one chunk of bytearray.
def encode(*data) -> bytearray:
    pass

# Decodes the input buffer and returns only the first item of the result.
def decode(buf: bytearray) -> any:
    pass

# Continuously decodes input buffers and yields any decoded data during the
# process.
def decode(buf: bytearray, temp: list) -> iter:
    pass

Example

import bsp

# encode and decode string
buf = bsp.encode("Hello, World!")
res = bsp.decode(buf) # Hello, World!

# encode and decode number
buf = bsp.encode(12345)
res = bsp.decode(buf) # 12345

# encode and decode boolean and None
buf = bsp.encode(True)
res = bsp.decode(buf) # True
buf = bsp.encode(False)
res = bsp.decode(buf) # False
buf = bsp.encode(None)
res = bsp.decode(buf) # None

# encode and decode dict
buf = bsp.encode({ "foo": "bar" })
res = bsp.decode(buf) # { "foo": "bar" }

# encode and decode list
buf = bsp.encode(["foo", "bar"])
res = bsp.decode(buf) # ["foo", "bar"]

# encode and decode binary
buf = bsp.encode(bytearray([1, 2, 3, 4, 5]))
res = bsp.decode(buf) # bytearray(b'\x01\x02\x03\x04\x05')


# encode and decode multiple data at once
buf = bsp.encode(
    "Hello, World!",
    12345,
    12345.678,
    1111111111111111111111111111111111111,
    True,
    False,
    None,
    { "foo": "bar" },
    ["foo", "bar"],
    bytearray([1, 2, 3, 4, 5])
)

# temp=[None]*3 must be provided in this case
for item in bsp.decode(buf, [None]*3):
    print(item) # will print the data accordingly


# Encode a large file and split it into chunks, then decode the chunks as a
# stream.
file = open("test.bin", "rb")
src = bytearray(file.read()) # file.read() returns bytes, must cast to bytearray
buf = bsp.encode(src)
segments = []
temp = [None]*3 # use a temp outside the stream processor to keep imcomplete data

# Split the file into several segments, each contians 64kb data of maximum length
for i in range(0, len(buf), 65535):
    segments.append(buf[i:i+65535])

# simulate a stream of segments
for segment in segments:
    for item in bsp.decode(segment, temp):
        print(item) # only run once in this case and print the only input (src)

bytes vs. bytearray

As being addressed above, when reading binary from a file, file.read() returns type of bytes, and even more pratically, it's more easy for a person to create a bytes instance than creating a bytearray, just use the b prefix for string.

However, this type will not be compatibal with the original Node.js BSP, and it's hard to determine whether it should be encoded as string or as binary. So, for more rational concerns, this package does not support bytes straight forward, a user must be explictly cast it to either a string or a bytearray according to what he needs.

For more detail about BSP, see bsp.

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

bsp-1.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

bsp-1.0.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file bsp-1.0.1.tar.gz.

File metadata

  • Download URL: bsp-1.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for bsp-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2f83ca231039f77878f59276d9dc0be049aeff0ef2a7d3639ca25a83578ec1cc
MD5 350773a2bcc794e2ad66cfc909fef6ed
BLAKE2b-256 bf019729203c41ff2fa5912dd36532e3c34de1461355ce10f57661c0bf2342ef

See more details on using hashes here.

File details

Details for the file bsp-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: bsp-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.5.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for bsp-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 47c72021ca9fe2a48974f9429879eca514df8e4e6819a9101ee0037af7436b55
MD5 a037e7df92b5afc8ba15c12be8687dd0
BLAKE2b-256 be85da18cf10305669055d2cb5abf83ec10cd4273aaaa80ae388c0ad159df94e

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