file modes

Dave Berkeley dave at rotwang.freeserve.co.uk
Tue Feb 1 17:06:41 CET 2000


Hi All

I'm trying to write a CGI script that returns image data instead of HTML.
The problem is that sys.stdout is opened in text mode, and I need to write
in binary. As a result each embedded "\n" in the image data gets expanded
into "\r\n", corrupting the image. I have tried the following:

out = os.fdopen(sys.stdout.fileno(), "wb")
out.write("Content-Type: application/octet-stream\r\n\r\n")
out.write(data)

But the "wb" mode does not seem to override the underlying mode of stdout.
I'm using Python 1.5.2 on Win 98 SE, talking to Apache 1.3.9.

How can I write to stdout in binary mode?

Dave Berkeley






More information about the Python-list mailing list