Python3 buffer extra byte??

Antoine Pitrou solipsis at pitrou.net
Mon May 17 14:46:12 EDT 2010


On Mon, 17 May 2010 20:34:21 +0200
Dodo <dodo_do_not_wake_up at 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")






More information about the Python-list mailing list