random byte string in python
An alternative way to obtaining a secure random sequence of bytes could be to use the standard library secrets
module, available since Python 3.6.
Example, based on the given question:
import secrets
b"\x00" + secrets.token_bytes(4) + b"\x00"
More information can be found at: https://docs.python.org/3/library/secrets.html
>>> import os
>>> "\x00"+os.urandom(4)+"\x00"
'\x00!\xc0zK\x00'