473,467 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

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

buffer = file1.read(256)
while buffer:
file2.write(buffer)
buffer = file1.read(256)

file1.close()
file2.close()

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' + \
file1.read())

file1.close()

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
1 3234
Well, here's the first page turned up by google for the terms 'python
binary stdout':
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65443
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!

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFB2fm1Jd01MZaTXX0RAoe/AJ0ZxufcwaA2efaQFsmJoX15uykOtgCeKAPA
FfccLcye/ave6u1HESzuNME=
=wLwt
-----END PGP SIGNATURE-----

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Miktor | last post by:
At the minute I am using the following rather unwieldy function to prevent the console window in Dev C++ from closing before I can see the output from my program: // function to prevent the...
1
by: Daniel | last post by:
when writing out a file from .net, when is the file created? after the bytes are all written to the hard drive or before the bytes are written to the hard drive?
2
by: ertanasan1 | last post by:
Hi, //Returns a new dataset with tree datatables. dsTemp.ReadXmlSchema(@"C:\mysch.xsd"); //Add a row to data table 0 DataRow dr = dsTemp.Tables.NewRow(); dr = "value1";...
2
by: John Salerno | last post by:
If I read a string that contains a newline character(s) into a variable, then write that variable to a file, how can I retain those newline characters so that the string remains on one line rather...
2
by: Fred Dag | last post by:
Hi, When writing using both these methods ( StreamWriter.Write("\n") vs WriteLine() ) to a file and open in Notepad I see a special character instead of newlines with Write("\n") vs a newline...
4
by: peterv6 | last post by:
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact...
1
by: sven _ | last post by:
Keywords: subprocess stdout stderr unbuffered pty tty pexpect flush setvbuf I'm trying to find a solution to <URL:http://bugs.python.org/issue1241>. In short: unless specifically told not to,...
2
by: =?Utf-8?B?S3VtYXI=?= | last post by:
I am using granados telnet client for connecting to the telnet and get the data from it. Every thing appears to be going smooth. But for some reason when I try to write the byte data to a string or...
1
by: Michom | last post by:
Hi all, I am new to perl and I have written a smal script to grab data from one file, and put them into another file. The problem is new lines, which are printing nice under a linux environment, but...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.