By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Controlling newlines when writing to stdout (no \r\n).

P: n/a
While I can make a verbatim copy of a file like this:

file1 = file('ready.pdf', 'rb')
file2 = file('out.pdf', 'wb')

buffer =
while buffer:
buffer =


I cannot send a verbatim copy to stdout. Python replaces
\n with \r\n due to its universal newline support. (This
is on Windows).

My aim is to send a binary file from a CGI python script.
I do this:

file1 = file('ready.pdf', 'rb')

sys.stdout.write( \
'Content-type: application/pdf\n' + \
'Content-disposition: inline; filename=ready.pdf\n\n' + \


Checking the traffic with my proxy server reveals that
inside the PDF file, all the \n chars have been replaced
with \r\n.

Is there a way to avoid this intervention?
(I avoided the whole problem by sending a HTTP redirect
'Location: ready.pdf\n\n', but I still want to know the answer).

Evgeni Sergeev
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
Well, here's the first page turned up by google for the terms 'python
binary stdout':
Code from that page:
import sys

if sys.platform == "win32":
import os, msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

Thanks, Google & the Python Cookbook website!


Version: GnuPG v1.2.6 (GNU/Linux)


Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.