sys.stdout.write not working properly for binary files on windows
I have a python script that handles all the incoming requests and generates/returns content based on the type of input.
When I run my python script with
print "Content-Type: text/plain\r\n\r\n" #debug mode
The generated html is what I expect to be. But, when I comment the above line, it truncates a part o开发者_JAVA技巧f the output. I generate the html by reading a file and using sys.stdout.write
to write to the output.
Code links:
driver: http://pastebin.com/VULgJWEx (contains the print statement)
Handler: http://pastebin.com/j87rrQyx (contains writeFileToStdout which writes to stdout)
Sample:
with Content-type: text -
Content-Length: 105
Content-Type: application/vnd.apple.mpegurl
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:118394
#EXTINF:5,
20110203T230818-01-118403.ts
without print:
Content-Length: 105
Content-Type: application/vnd.apple.mpegurl
#EXTM3U
#EXT-X-TARGETDURATION:10
#EXT-X-MEDIA-SEQUENCE:118394
#EXTINF:5,
20110203T230818-01-118403 #(ts is missing!!)
The last ts is truncated.
Would anyone have a clue why the print is causing this ? (or is it something else?)
There was not a problem in print or stdout (as expected).
I was trying to read from a binary file (on windows) and write it to stdout. Since the 'sys' module opens the 'stdout' file object on your behalf and normally does so in text mode, things were going haywire.
So, to fix this problem:
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
This link does a good job of explaining the problem.
精彩评论