PySocks
PySocks copied to clipboard
Fix length field conversion
Since urllib3 removed their fork of the library again, I'm reposting this here in the hope that it will eventually be picked up.
The chr() method interprets its argument as Unicode code point. Consequently, chr(x).encode() only works for encoding (unsigned) integers with values between 0x00..0x7F (ASCII subset) into bytes, but not for values between 0x80..0xFF. However, single-byte length fields currently encoded using chr(x).encode() are unsigned 1-byte integers with values between 0..255 (0x00..0xFF).
This PR improves conversion between integers and byte-strings by relying on the pack/unpack methods of the struct standard library to get consistent encodeding and decoding of integer values with well-defined format.