How to combine incoming byte chunks
I asked about this on StackOverflow , and then I had to look up the answer again on another occasion. So I'll list it here :). To safe you some clicks, there are two options for the byte storage:
Instantiating an empty byte string,
b'', and appending / concatenating. This incurs a bit of string copy overhead.
- Instantiating a new empty byte array, and then extending it (appending to it).
The code might look as follows:
def process_incoming_connection(conn, addr): byte_array = bytearray() while 1: chunk = conn.recv(1024) if not chunk: break byte_array.extend(chunk) message = byte_array.decode(encoding='utf-8'); conn.close() # Now we can do something with the message
How to manually update or patch an official Python package
Sometimes we might need to do a bugfix on a Python package, or we need to use the latest version from the repository that is not yet packaged for our platform. How can we install this new version? This is very simple, we only need to copy the source files.
For example, if we want to update our system version of a package 'anyplugin' that lives in the 'pelican plugins' namespace, we simply overwrite the system version with our source files.
cp -r anyplugin /usr/local/lib/python3.8/site-packages/pelican/plugins/
The directory we copy is the actual python source file directory, this is not necessarily the directory root.