Writing byte stream as jpeg format to disk
bryanjugglercryptographer at yahoo.com
Fri Aug 27 08:56:26 CEST 2010
Robert Kern wrote:
> follow our advice. Split using b'\r\n\r\n' and use the maxsplit=1 argument to
> make sure that you do not split on spurious b'\r\n\r\n' sequences inside the
> JPEG body. Do not decode the bytes.
Correct, and I'll add that this is a case where we might want to be
better than correct. BaseHTTPRequestHandler in the Python standard
library accommodates clients that incorrectly omit the '\r' and end
header lines with just '\n'. Such apps have been seen in the wild.
Since bare '\n' never appears in correctly formed HTTP headers,
interpreting it as equivalent to '\r\n' doesn't break anything.
The re module offers a split that does what we want.
boundary_re = re.compile(br'\r?\n\r?\n')
then you can use:
(headers, content) = boundary_re.split(rawdata, 1)
I like Robert's suggestion to use the HTTP server or wsgiref in the
Python library. There's significant arcane wisdom programmed in
More information about the Python-list