Sending binary data over CGI

Discussion in 'Python' started by Walter Huf, Mar 4, 2004.

  1. Walter Huf

    Walter Huf Guest

    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--



    -----= 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! =-----
     
    Walter Huf, Mar 4, 2004
    #1
    1. Advertising

  2. Walter Huf

    Peter Otten Guest

    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/python-list/2003-May/161515.html
    http://mail.python.org/pipermail/python-list/2003-May/161319.html

    Peter
     
    Peter Otten, Mar 4, 2004
    #2
    1. Advertising

  3. Walter Huf

    Tim Roberts Guest

    Walter Huf <> 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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Mar 4, 2004
    #3
  4. Walter Huf

    Walter Huf Guest

    Tim Roberts <> wrote in
    news:eek::
    >
    > 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--



    -----= 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! =-----
     
    Walter Huf, Mar 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan Li?e
    Replies:
    6
    Views:
    782
    Steve Horsley
    Feb 8, 2004
  2. Replies:
    9
    Views:
    1,103
    Simon Forman
    Jul 4, 2006
  3. YT
    Replies:
    1
    Views:
    271
    Alan Howard
    Apr 30, 2004
  4. AirYT
    Replies:
    0
    Views:
    198
    AirYT
    Jan 24, 2006
  5. SteveC
    Replies:
    0
    Views:
    125
    SteveC
    Jul 13, 2005
Loading...

Share This Page