Help: Uploading .zip to Python CGI

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Will Stuyvesant

    Help: Uploading .zip to Python CGI

    I am uploading a .zip file to a Python CGI program, using a form on
    a HTML page with

    <input name="yourfile" type="file">...

    In the Python CGI program I do:

    import cgi
    fStorage = cgi.FieldStorag e()
    zipdata = fStorage['yourfile'].value
    print "Content-type: text/plain"
    print
    print len(zipdata)

    Now the length of the zipdata is 100, where it should be about
    2635...and unzipping it with zipfile of course gives the "not a zip
    file" error.

    The last part of the data that is received by the CGI script is:

    \xf2\xf1!0\xdbS \xa9

    and the next one *should* be \x1a

    It seems that the .zip data is being truncated, but I don't know where
    in my tool chain.


    The strange thing is that the Python CGI script *does* work on a
    Apache 1.3.27 server at work (unix), but gives the error above when
    run on
    my laptop with Windows XP and Apache 1.3.27 and also with the Apache
    version 2.0.48 I tried later.

    Does anybody have a clue what is going on?

    Maybe the error is with the Windows version of Apache? Or is it a
    Python problem (the unix server has Python 2.1.1).
  • Jay Dorsey

    #2
    Re: Help: Uploading .zip to Python CGI

    On Sat, Dec 06, 2003 at 12:40:43PM -0800, Will Stuyvesant wrote:[color=blue]
    > I am uploading a .zip file to a Python CGI program, using a form on
    > a HTML page with
    >[/color]
    <snip>

    Without being able to see the form, I wonder if you're certain you set the enctype on the form to "multipart/form-data"?

    You're working across multiple servers and if you're retyping the script each time its easy to forget the enctype of the form.

    HTH


    --
    Jay Dorsey
    jay at jaydorsey dot com

    Comment

    • Will Stuyvesant

      #3
      Re: Help: Uploading .zip to Python CGI

      The problem I described in this thread is with Apache, not with
      Python! And the unix Apache at my work has no problems, its only the
      Windows Apache versions. So the Apache peeps will probably say it's a
      *Windows* problem 0-)

      I found out with the following: I can now avoid the first HTML page
      with the .zip upload, instead I upload the .zip to my Python CGI
      program with this little program:


      import urllib
      import webbrowser

      webserviceURI = r'http://localhost/cgi-bin/mycgiprogram.py '
      startpageName = 'start.xml'


      # instead of a HTML page with INPUT type=file just read the file
      fp = open(fname, 'rb')
      data = fp.read()
      fp.close()

      # all CGI parameters in a dict, and encoded
      params = urllib.urlencod e({ 'yourfile': data })

      # call the CGI program and read what it returns
      f = urllib.urlopen( webserviceURI, params)
      webpage = f.read()

      # save it locally in a file
      wpfp = open(startpageN ame, 'w')
      wpfp.write(webp age)
      wpfp.close()

      # show it in your browser
      webbrowser.open (startpageName)


      --
      If pro is the opposite of con, what is the opposite of progress?

      Comment

      • jmdeschamps

        #4
        Re: Help: Uploading .zip to Python CGI

        hwlgw@hotmail.c om (Will Stuyvesant) wrote in message news:<cb035744. 0312061240.76b9 02f5@posting.go ogle.com>...[color=blue]
        > I am uploading a .zip file to a Python CGI program, using a form on
        > a HTML page with
        >
        > <input name="yourfile" type="file">...
        >
        > In the Python CGI program I do:
        >
        > import cgi
        > fStorage = cgi.FieldStorag e()
        > zipdata = fStorage['yourfile'].value
        > print "Content-type: text/plain"
        > print
        > print len(zipdata)
        >
        > Now the length of the zipdata is 100, where it should be about
        > 2635...and unzipping it with zipfile of course gives the "not a zip
        > file" error.
        >
        > The last part of the data that is received by the CGI script is:
        >
        > \xf2\xf1!0\xdbS \xa9
        >
        > and the next one *should* be \x1a
        >
        > It seems that the .zip data is being truncated, but I don't know where
        > in my tool chain.
        >[/color]
        ....[color=blue]
        > Does anybody have a clue what is going on?
        >
        > Maybe the error is with the Windows version of Apache? Or is it a
        > Python problem (the unix server has Python 2.1.1).[/color]

        Had a similare problem with *.jpg uploads

        uploading files with a shebang such as:
        #! c:/python23/python -u
        reolved it for me
        the -u part telling Windows to get data "unbuffered ", so I read somewhere...

        Good weekend,

        JM

        Comment

        • Will Stuyvesant

          #5
          Re: Help: Uploading .zip to Python CGI

          > [jmdeschamps@cvm .qc.ca][color=blue]
          > Had a similare problem with *.jpg uploads
          >
          > uploading files with a shebang such as:
          > #! c:/python23/python -u
          > reolved it for me
          > the -u part telling Windows to get data "unbuffered ", so I read somewhere...[/color]

          Great! Solved it for me too! Thank you!

          Comment

          • Robin Munn

            #6
            Re: Help: Uploading .zip to Python CGI

            Will Stuyvesant <hwlgw@hotmail. com> wrote:[color=blue]
            > I am uploading a .zip file to a Python CGI program, using a form on
            > a HTML page with
            >
            > <input name="yourfile" type="file">...
            >
            > In the Python CGI program I do:
            >
            > import cgi
            > fStorage = cgi.FieldStorag e()
            > zipdata = fStorage['yourfile'].value
            > print "Content-type: text/plain"
            > print
            > print len(zipdata)
            >
            > Now the length of the zipdata is 100, where it should be about
            > 2635...and unzipping it with zipfile of course gives the "not a zip
            > file" error.
            >
            > The last part of the data that is received by the CGI script is:
            >
            > \xf2\xf1!0\xdbS \xa9
            >
            > and the next one *should* be \x1a[/color]

            \x1a is ASCII for Ctrl-Z, which is used in Windows as an EOF (End Of
            File) marker.
            [color=blue]
            > It seems that the .zip data is being truncated, but I don't know where
            > in my tool chain.[/color]

            Somewhere in your tool chain, something is opening a file in text mode
            instead of in binary mode.

            The fact that your CGI script works on Unix but fails on Windows is
            further proof that the Ctrl-Z is being treated as EOF on Windows, since
            Unix doesn't give that character any special meaning.

            --
            Robin Munn
            rmunn@pobox.com

            Comment

            • Andrew MacIntyre

              #7
              Re: Help: Uploading .zip to Python CGI

              On Sun, 6 Dec 2003, Will Stuyvesant wrote:
              [color=blue]
              > The last part of the data that is received by the CGI script is:
              >
              > \xf2\xf1!0\xdbS \xa9
              >
              > and the next one *should* be \x1a[/color]

              ISTR that \x1A is control-Z.

              Which is the EOF character on CP/M derived systems, and is still
              interpreted thusly in the most surprising places in software from Redmond.

              As another poster suggested, the -u option is the usual solution.

              --
              Andrew I MacIntyre "These thoughts are mine alone..."
              E-mail: andymac@bullsey e.apana.org.au (pref) | Snail: PO Box 370
              andymac@pcug.or g.au (alt) | Belconnen ACT 2616
              Web: http://www.andymac.org/ | Australia

              Comment

              Working...