Fix the header writer for a wire protocol
Your task
Your service sends a 6-byte header: a 32-bit sequence number, then a 16-bit port. The first version wrote the bytes in the host's native order, so it round-tripped in one process but every peer on another machine misread it. The fix writes both fields in network byte order, which is big-endian.
An assistant wrote write_wire_header(seq, port). It is in the editor. It has a bug. Find it and fix it.
Input and output:
seq: the sequence number, an integer.port: the port, an integer.- Return the 6 header bytes as a list of integers, or
[]when a value does not fit its field.
Rules the tests check:
seqmust be from 0 to 4294967295 (it fills 4 bytes) andportfrom 0 to 65535 (2 bytes). If either is out of range, return[]. Never wrap a value to make it fit.- Otherwise return the 4 bytes of
seq, then the 2 bytes ofport, each field most significant byte first.
Examples
write_wire_header(seq: int, port: int) -> list[int]Example 1
Input
write_wire_header(1, 80)Output
[0, 0, 0, 1, 0, 80]Example 2
Input
write_wire_header(305419896, 80)Output
[18, 52, 86, 120, 0, 80]Example 3
Input
write_wire_header(0, 20480)Output
[0, 0, 0, 0, 80, 0]