A module for encoding and decoding netstrings.
Project description
A module for encoding and decoding netstrings. See the definition of netstrings at https://cr.yp.to/proto/netstrings.txt.
Requirements
Pynetstring is written for Python 3.
Usage
Encoding
netstring_bytes = netstring.encode('Hello') # Will give b'Hello' print(netstring_bytes)
Decoding
In the simplest case, when we know for sure that the data we are trying to decode ends on a netstring boundary we can simply do:
# data_list will be an list of bytes. decoded_list = netstring.decode('5:Hello,6:World!') # Will give [b'Hello', b'World!'] print(decoded_list)
In many cases however, such when netstring data is transmitted over a network, the chunks of data that arrive may not align to netstring boundaries. For example a chunk of data may contain a netstring and then parts of the next. To handle this, call Decoder.feed(), which will parse the data and emit decoded data as soon as it has been fully received.
decoder = netstring.Decoder() # Will give [] print(decoder.feed('5:He')) # Will give [b'Hello', b'World!'] print(decoder.feed('llo,6:World!,5:ag')) # Will give [b'again'] print(decoder.feed('ain,'))
The receiving side could look something like this:
def handle_network_data(data): decoder = netstring.Decoder() while True: decoded_list = decoder.feed(data) for item in decoded_list: print(item)
Data encoding
Regardless of the type of the data that is sent to encode(), it will always return binary data, i.e. python bytes. The data that is returned from decode() and Decoder.feed() will also be binary. This is because the decoder can not make any assumptions on the encoding of the data. If you know that the data that comes in can be interpreted in a particular way or encoding, e.g. UTF-8, you have to explicitly do that conversion.
encoded = netstring.encode(u'Hello World!') # This will be <class 'bytes'> print(type(encoded)) decoded_list = netstring.decode(encoded) # This will be <class 'bytes'> print(type(decoded_list[0])) # This will give the python string Hello World! print(decoded_list[0].decode('utf-8'))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pynetstring-0.1.dev2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 275c078175d9bbbe4941d768219a37d95154ebb46af8163f0e3dcfb8ce486939 |
|
MD5 | c8d3e5c7f9e56b613089d72c0ca1171f |
|
BLAKE2b-256 | 93682bd5e7ebf0042ece3d4dccc49b5485d630d8db2a059d4aea3b2f74fbf3d5 |