Writing byte stream as jpeg format to disk

Bryan bryanjugglercryptographer at yahoo.com
Fri Aug 27 08:56:26 CEST 2010


Robert Kern wrote:
> Please
> 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.

  import re
  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
already.



More information about the Python-list mailing list