From: Dodo on
Let's consider this code:

#!/usr/bin/python3
import cgi, sys
print("Content-type:image/jpeg\n\n")
f = open("img.jpg","rb")
sys.stdout.flush()
sys.stdout.buffer.write( f.read() )
f.close()

I receive the file with one padding byte at the start of the file (0x0a)
http://www.1pix.org/multi/images/wg7zg58gsgbhc9cppo5i.jpg

Any idea why?

Dorian
(yes, this is the continuation of "CGI python 3 write RAW BINARY")
From: Antoine Pitrou on
On Mon, 17 May 2010 20:34:21 +0200
Dodo <dodo_do_not_wake_up(a)yahoo.Fr> wrote:

> Let's consider this code:
>
> #!/usr/bin/python3
> import cgi, sys
> print("Content-type:image/jpeg\n\n")

print() adds an additional \n, so there's one too many.
Also, HTTP headers should be separated with \r\n, not \n.

(besides, under Windows \n will be converted to \r\n by the text I/O
layer, therefore, it would be better to use the binary I/O layer, a.k.a
sys.stdout.buffer, if you want your script to be portable)

Therefore, I would advocate rewriting it as:

sys.stdout.buffer.write(b"Content-type:image/jpeg\r\n\r\n")