Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Sending binary data over CGI

Reply
Thread Tools

Sending binary data over CGI

 
 
Walter Huf
Guest
Posts: n/a
 
      03-04-2004
Okay, I'm busy making a wonderful cgi program, and everything is working
wonderfully so far. I'm using the standard cgi library and the Cookie
library and they have been a huge help. Thanks to those who have created
them!
However, if I try to send binary data, like jpgs, through cgi, the data
gets corrupted in a very small manner. Whenever my program sends the
character 10, it gets converted to character 13+character 10. I found out
this only happens when I send data to stdout. If I send the data to a
standard file object, it works fine. However, with the stdout, the problem
arises.
Here is the relevant part of the code:
---Begin
from sys import stdout
def senddownloaddata(file): #file is a pointer to an open file
line=file.read(1) #file pointer opening and closing are
while len(line)==1: # handled outside of function
stdout.write(line)
line=file.read(1)
stdout.write(line)
---End
Now the code that copies files:
---Begin
curdir="C:\windows\desktop"
temp=file(curdir+"/bach.it","rb",0)
out=file(curdir+"/bach2.it","wb",0)
data=temp.read(1)
while len(data)==1:
out.write(data)
data=temp.read(1)
temp.close()
out.close()
---End

I'm using Windows 98 SE with an Apache 2.0.44 Win32 server. That all works
fine. The first line of the interactive script says:
PythonWin 2.3.2 (#49, Nov 13 2003, 10:34:54) [MSC v.1200 32 bit (Intel)] on
win32.
I'm assuming that means I have Python 2.3.2. Everything else in the program
works very nice, except for the data corruption.

Please help me with this problem. It has me stumped.
Thanks in advance!

--Walter Huf--
http://www.velocityreviews.com/forums/(E-Mail Removed)


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-04-2004
Walter Huf wrote:

> However, if I try to send binary data, like jpgs, through cgi, the data
> gets corrupted in a very small manner. Whenever my program sends the
> character 10, it gets converted to character 13+character 10. I found out
> this only happens when I send data to stdout. If I send the data to a
> standard file object, it works fine. However, with the stdout, the problem
> arises.


Have you tried groups.google.com? Searching c.l.py for "stdout binary"
should suffice. For now:

http://mail.python.org/pipermail/pyt...ay/161515.html
http://mail.python.org/pipermail/pyt...ay/161319.html

Peter
 
Reply With Quote
 
 
 
 
Tim Roberts
Guest
Posts: n/a
 
      03-04-2004
Walter Huf <(E-Mail Removed)> wrote:
>
>Okay, I'm busy making a wonderful cgi program, and everything is working
>wonderfully so far. I'm using the standard cgi library and the Cookie
>library and they have been a huge help. Thanks to those who have created
>them!
>However, if I try to send binary data, like jpgs, through cgi, the data
>gets corrupted in a very small manner. Whenever my program sends the
>character 10, it gets converted to character 13+character 10. I found out
>this only happens when I send data to stdout. If I send the data to a
>standard file object, it works fine. However, with the stdout, the problem
>arises.
>...
>Please help me with this problem. It has me stumped.


Really? This is such a common problem that most Windows Python programmers
encounter it very early in their experiments.

The issue, of course, is that stdout is opened as a text file, not as a
binary file, and we all know that the LF to CR-LF conversion you describe
is part of the normal processing of a text file in Windows.

If you must send binary data through stdout, do this before you start to
write:

import os
import msvcrt
...
msvcrt.setmode( stdout.fileno(), os.O_BINARY )
--
- Tim Roberts, (E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
Walter Huf
Guest
Posts: n/a
 
      03-04-2004
Tim Roberts <(E-Mail Removed)> wrote in
news(E-Mail Removed):
>
> Really? This is such a common problem that most Windows Python
> programmers encounter it very early in their experiments.
>
> The issue, of course, is that stdout is opened as a text file, not as
> a binary file, and we all know that the LF to CR-LF conversion you
> describe is part of the normal processing of a text file in Windows.
>
> If you must send binary data through stdout, do this before you start
> to write:
>
> import os
> import msvcrt
> ...
> msvcrt.setmode( stdout.fileno(), os.O_BINARY )


Awesome, thanks! I'm quite new at Python. I've done a lot in Visual Basic,
and even written a couple cgiprogs in basic, but my server on the
internet(http://hufman.cobalty.com) is a linux server, and, Micro$oft being
Micro$oft, there is no visual basic compiler or interpreter for linux. I
started learning Python last Sunday, so I wouldn't know anything about the
conversions. Well, I knew about the newline conversions, I think, but I
didn't know how to get around it. I couldn't find anything about it in the
help. Thank you though!

--Walter Huf--
(E-Mail Removed)


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
sending binary data over sockets thorley@gmail.com Python 9 07-04-2006 01:51 AM
Sending binary file (PDF) over SSL using ASP script AirYT ASP General 0 01-24-2006 08:47 PM
sending binary data from rcairo through cgi SteveC Ruby 0 07-13-2005 12:35 PM
sending binary file using asp over ssl YT ASP General 1 04-30-2004 09:32 PM



Advertisments