File Uploads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug Helm

    #1

    File Uploads

    Hey, Folks:

    I'm trying to write a very simple file upload CGI. I'm on a Windows server.
    I *am* using the -u switch to start Python for CGIs, as follows:

    c:\python\pytho n.exe -u %s %s

    I *do* have write permissions on the directory I'm trying to write to. But,
    when I click submit, it just hangs. Any help would be greatly appreciated.
    Thanks. Here's the code...

    Upload.py

    import cgi

    print "content-type: text/html\n\n"

    form = cgi.FieldStorag e()
    if not form:
    print """
    <html>
    <head></head>
    <body>
    <form name="frmMain" action="Upload. py" method="POST"
    enctype="multip art/form-data">
    <input type="file" name="filename" ><br>
    <input type="submit">
    </form>
    </body>
    </html>
    """
    else:
    import BLOB
    lobjUp = BLOB.BLOB()
    if lobjUp.Save('fi lename', 'SomeFile.jpg') :
    print """
    <html>
    <head></head>
    <body>
    File successfully saved.
    </body>
    </html>
    """
    else:
    print """
    <html>
    <head></head>
    <body>
    Unable to save file.
    </body>
    </html>
    """

    --------------

    Blob.py

    import cgi
    import staticobject

    cTrue = 1
    cFalse = 0

    try:
    import msvcrt,os
    msvcrt.setmode( 0, os.O_BINARY ) # stdin = 0
    msvcrt.setmode( 1, os.O_BINARY ) # stdout = 1
    except ImportError:
    pass


    class BLOB(staticobje ct.StaticObject ):

    def __init__(self):
    self.initializi ng = cTrue
    staticobject.St aticObject.__in it__(self)
    self.initializi ng = cFalse

    def Save(self, pstrFormFieldNa me, pstrFilePathAnd Name):

    # tried this first -- same result -- just hangs...
    # try:
    # form = cgi.FieldStorag e()
    # item = form[pstrFormFieldNa me]
    # if item.file:
    # data = item.file.read( )
    # f = open(pstrFilePa thAndName,'wb')
    # f.write(data)
    # f.close()
    # return cTrue
    # else:
    # return cFalse
    # except:
    # return cFalse

    form = cgi.FieldStorag e()
    f = open(pstrFilePa thAndName,'wb')
    f.write(form[pstrFormFieldNa me].value)
    f.close()


  • dimitri pater

    #2
    Re: File Uploads

    Maybe this helps:
    http://www.voidspace.org.uk/python/cgi.shtml#upload

    I use it, it works for fine me
    Maybe it will give you some clues on how to tweak your own script.

    Dimitri


    On Sun, 27 Mar 2005 10:32:20 -0700, Doug Helm <dhelm@wcsoftwa re.com> wrote:[color=blue]
    > Hey, Folks:
    >
    > I'm trying to write a very simple file upload CGI. I'm on a Windows server.
    > I *am* using the -u switch to start Python for CGIs, as follows:
    >
    > c:\python\pytho n.exe -u %s %s
    >
    > I *do* have write permissions on the directory I'm trying to write to. But,
    > when I click submit, it just hangs. Any help would be greatly appreciated.
    > Thanks. Here's the code...
    >
    > Upload.py
    >
    > import cgi
    >
    > print "content-type: text/html\n\n"
    >
    > form = cgi.FieldStorag e()
    > if not form:
    > print """
    > <html>
    > <head></head>
    > <body>
    > <form name="frmMain" action="Upload. py" method="POST"
    > enctype="multip art/form-data">
    > <input type="file" name="filename" ><br>
    > <input type="submit">
    > </form>
    > </body>
    > </html>
    > """
    > else:
    > import BLOB
    > lobjUp = BLOB.BLOB()
    > if lobjUp.Save('fi lename', 'SomeFile.jpg') :
    > print """
    > <html>
    > <head></head>
    > <body>
    > File successfully saved.
    > </body>
    > </html>
    > """
    > else:
    > print """
    > <html>
    > <head></head>
    > <body>
    > Unable to save file.
    > </body>
    > </html>
    > """
    >
    > --------------
    >
    > Blob.py
    >
    > import cgi
    > import staticobject
    >
    > cTrue = 1
    > cFalse = 0
    >
    > try:
    > import msvcrt,os
    > msvcrt.setmode( 0, os.O_BINARY ) # stdin = 0
    > msvcrt.setmode( 1, os.O_BINARY ) # stdout = 1
    > except ImportError:
    > pass
    >
    > class BLOB(staticobje ct.StaticObject ):
    >
    > def __init__(self):
    > self.initializi ng = cTrue
    > staticobject.St aticObject.__in it__(self)
    > self.initializi ng = cFalse
    >
    > def Save(self, pstrFormFieldNa me, pstrFilePathAnd Name):
    >
    > # tried this first -- same result -- just hangs...
    > # try:
    > # form = cgi.FieldStorag e()
    > # item = form[pstrFormFieldNa me]
    > # if item.file:
    > # data = item.file.read( )
    > # f = open(pstrFilePa thAndName,'wb')
    > # f.write(data)
    > # f.close()
    > # return cTrue
    > # else:
    > # return cFalse
    > # except:
    > # return cFalse
    >
    > form = cgi.FieldStorag e()
    > f = open(pstrFilePa thAndName,'wb')
    > f.write(form[pstrFormFieldNa me].value)
    > f.close()
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >[/color]


    --
    Please visit dimitri's website: www.serpia.com

    Comment

    • Doug Helm

      #3
      Re: File Uploads

      Thanks, Dimitri. Yes, I found that same code too and tried it with the
      exact same result as the code I've uploaded (just hangs). But, OK. You
      have it working, so it must be a systems issue. Are you also on a Windows
      IIS web server? Do you have CGI configured the same way (i.e. .py =
      python.exe -u %s %s)?

      Thanks.

      Doug

      "dimitri pater" <dimitri.pater@ gmail.com> wrote in message
      news:mailman.92 4.1111960549.17 99.python-list@python.org ...[color=blue]
      > Maybe this helps:
      > http://www.voidspace.org.uk/python/cgi.shtml#upload
      >
      > I use it, it works for fine me
      > Maybe it will give you some clues on how to tweak your own script.
      >
      > Dimitri
      >
      >
      > On Sun, 27 Mar 2005 10:32:20 -0700, Doug Helm <dhelm@wcsoftwa re.com>[/color]
      wrote:[color=blue][color=green]
      > > Hey, Folks:
      > >
      > > I'm trying to write a very simple file upload CGI. I'm on a Windows[/color][/color]
      server.[color=blue][color=green]
      > > I *am* using the -u switch to start Python for CGIs, as follows:
      > >
      > > c:\python\pytho n.exe -u %s %s
      > >
      > > I *do* have write permissions on the directory I'm trying to write to.[/color][/color]
      But,[color=blue][color=green]
      > > when I click submit, it just hangs. Any help would be greatly[/color][/color]
      appreciated.[color=blue][color=green]
      > > Thanks. Here's the code...
      > >
      > > Upload.py
      > >
      > > import cgi
      > >
      > > print "content-type: text/html\n\n"
      > >
      > > form = cgi.FieldStorag e()
      > > if not form:
      > > print """
      > > <html>
      > > <head></head>
      > > <body>
      > > <form name="frmMain" action="Upload. py" method="POST"
      > > enctype="multip art/form-data">
      > > <input type="file" name="filename" ><br>
      > > <input type="submit">
      > > </form>
      > > </body>
      > > </html>
      > > """
      > > else:
      > > import BLOB
      > > lobjUp = BLOB.BLOB()
      > > if lobjUp.Save('fi lename', 'SomeFile.jpg') :
      > > print """
      > > <html>
      > > <head></head>
      > > <body>
      > > File successfully saved.
      > > </body>
      > > </html>
      > > """
      > > else:
      > > print """
      > > <html>
      > > <head></head>
      > > <body>
      > > Unable to save file.
      > > </body>
      > > </html>
      > > """
      > >
      > > --------------
      > >
      > > Blob.py
      > >
      > > import cgi
      > > import staticobject
      > >
      > > cTrue = 1
      > > cFalse = 0
      > >
      > > try:
      > > import msvcrt,os
      > > msvcrt.setmode( 0, os.O_BINARY ) # stdin = 0
      > > msvcrt.setmode( 1, os.O_BINARY ) # stdout = 1
      > > except ImportError:
      > > pass
      > >
      > > class BLOB(staticobje ct.StaticObject ):
      > >
      > > def __init__(self):
      > > self.initializi ng = cTrue
      > > staticobject.St aticObject.__in it__(self)
      > > self.initializi ng = cFalse
      > >
      > > def Save(self, pstrFormFieldNa me, pstrFilePathAnd Name):
      > >
      > > # tried this first -- same result -- just hangs...
      > > # try:
      > > # form = cgi.FieldStorag e()
      > > # item = form[pstrFormFieldNa me]
      > > # if item.file:
      > > # data = item.file.read( )
      > > # f = open(pstrFilePa thAndName,'wb')
      > > # f.write(data)
      > > # f.close()
      > > # return cTrue
      > > # else:
      > > # return cFalse
      > > # except:
      > > # return cFalse
      > >
      > > form = cgi.FieldStorag e()
      > > f = open(pstrFilePa thAndName,'wb')
      > > f.write(form[pstrFormFieldNa me].value)
      > > f.close()
      > >
      > > --
      > > http://mail.python.org/mailman/listinfo/python-list
      > >[/color]
      >
      >
      > --
      > Please visit dimitri's website: www.serpia.com[/color]


      Comment

      • dimitri pater

        #4
        Re: File Uploads

        No, I am on a Linux server. I am not sure how CGI is configured
        because I do not control the server, I only use it.

        bye,
        Dimitri


        On Sun, 27 Mar 2005 16:19:00 -0700, Doug Helm <dhelm@wcsoftwa re.com> wrote:[color=blue]
        > Thanks, Dimitri. Yes, I found that same code too and tried it with the
        > exact same result as the code I've uploaded (just hangs). But, OK. You
        > have it working, so it must be a systems issue. Are you also on a Windows
        > IIS web server? Do you have CGI configured the same way (i.e. .py =
        > python.exe -u %s %s)?
        >
        > Thanks.
        >
        > Doug
        >
        > "dimitri pater" <dimitri.pater@ gmail.com> wrote in message
        > news:mailman.92 4.1111960549.17 99.python-list@python.org ...[color=green]
        > > Maybe this helps:
        > > http://www.voidspace.org.uk/python/cgi.shtml#upload
        > >
        > > I use it, it works for fine me
        > > Maybe it will give you some clues on how to tweak your own script.
        > >
        > > Dimitri
        > >
        > >
        > > On Sun, 27 Mar 2005 10:32:20 -0700, Doug Helm <dhelm@wcsoftwa re.com>[/color]
        > wrote:[color=green][color=darkred]
        > > > Hey, Folks:
        > > >
        > > > I'm trying to write a very simple file upload CGI. I'm on a Windows[/color][/color]
        > server.[color=green][color=darkred]
        > > > I *am* using the -u switch to start Python for CGIs, as follows:
        > > >
        > > > c:\python\pytho n.exe -u %s %s
        > > >
        > > > I *do* have write permissions on the directory I'm trying to write to.[/color][/color]
        > But,[color=green][color=darkred]
        > > > when I click submit, it just hangs. Any help would be greatly[/color][/color]
        > appreciated.[color=green][color=darkred]
        > > > Thanks. Here's the code...
        > > >
        > > > Upload.py
        > > >
        > > > import cgi
        > > >
        > > > print "content-type: text/html\n\n"
        > > >
        > > > form = cgi.FieldStorag e()
        > > > if not form:
        > > > print """
        > > > <html>
        > > > <head></head>
        > > > <body>
        > > > <form name="frmMain" action="Upload. py" method="POST"
        > > > enctype="multip art/form-data">
        > > > <input type="file" name="filename" ><br>
        > > > <input type="submit">
        > > > </form>
        > > > </body>
        > > > </html>
        > > > """
        > > > else:
        > > > import BLOB
        > > > lobjUp = BLOB.BLOB()
        > > > if lobjUp.Save('fi lename', 'SomeFile.jpg') :
        > > > print """
        > > > <html>
        > > > <head></head>
        > > > <body>
        > > > File successfully saved.
        > > > </body>
        > > > </html>
        > > > """
        > > > else:
        > > > print """
        > > > <html>
        > > > <head></head>
        > > > <body>
        > > > Unable to save file.
        > > > </body>
        > > > </html>
        > > > """
        > > >
        > > > --------------
        > > >
        > > > Blob.py
        > > >
        > > > import cgi
        > > > import staticobject
        > > >
        > > > cTrue = 1
        > > > cFalse = 0
        > > >
        > > > try:
        > > > import msvcrt,os
        > > > msvcrt.setmode( 0, os.O_BINARY ) # stdin = 0
        > > > msvcrt.setmode( 1, os.O_BINARY ) # stdout = 1
        > > > except ImportError:
        > > > pass
        > > >
        > > > class BLOB(staticobje ct.StaticObject ):
        > > >
        > > > def __init__(self):
        > > > self.initializi ng = cTrue
        > > > staticobject.St aticObject.__in it__(self)
        > > > self.initializi ng = cFalse
        > > >
        > > > def Save(self, pstrFormFieldNa me, pstrFilePathAnd Name):
        > > >
        > > > # tried this first -- same result -- just hangs...
        > > > # try:
        > > > # form = cgi.FieldStorag e()
        > > > # item = form[pstrFormFieldNa me]
        > > > # if item.file:
        > > > # data = item.file.read( )
        > > > # f = open(pstrFilePa thAndName,'wb')
        > > > # f.write(data)
        > > > # f.close()
        > > > # return cTrue
        > > > # else:
        > > > # return cFalse
        > > > # except:
        > > > # return cFalse
        > > >
        > > > form = cgi.FieldStorag e()
        > > > f = open(pstrFilePa thAndName,'wb')
        > > > f.write(form[pstrFormFieldNa me].value)
        > > > f.close()
        > > >
        > > > --
        > > > http://mail.python.org/mailman/listinfo/python-list
        > > >[/color]
        > >
        > >
        > > --
        > > Please visit dimitri's website: www.serpia.com[/color]
        >
        > --
        > http://mail.python.org/mailman/listinfo/python-list
        >[/color]


        --
        Please visit dimitri's website: www.serpia.com

        Comment

        • and-google@doxdesk.com

          #5
          Re: File Uploads

          Doug Helm wrote:
          [color=blue]
          > form = cgi.FieldStorag e()
          > if lobjUp.Save('fi lename', 'SomeFile.jpg') :[/color]
          [color=blue]
          > class BLOB(staticobje ct.StaticObject ):
          > def Save(self, pstrFormFieldNa me, pstrFilePathAnd Name):
          > form = cgi.FieldStorag e()[/color]

          You are instantiating cgi.FieldStorag e twice. This won't work for POST
          requests, because instantiating a FieldStorage reads the form data from
          the standard input stream (the HTTP request).

          Try to create a second one and cgi will try to read all the form data
          again; this will hang, waiting for the socket to send it a load more
          data which will not be forthcoming.

          When using CGI, parse the input only once, then pass the results (a
          FieldStorage object if you are using the cgi module) in to any other
          functions that need to read it.

          --
          Andrew Clover
          mailto:and@doxd esk.com


          Comment

          • Doug Helm

            #6
            Re: File Uploads

            Andrew:

            I'm a dope. You're brilliant. Thank you. That worked splendidly.

            Doug

            <and-google@doxdesk. com> wrote in message
            news:1111988579 .390466.68750@g 14g2000cwa.goog legroups.com...[color=blue]
            > Doug Helm wrote:
            >[color=green]
            > > form = cgi.FieldStorag e()
            > > if lobjUp.Save('fi lename', 'SomeFile.jpg') :[/color]
            >[color=green]
            > > class BLOB(staticobje ct.StaticObject ):
            > > def Save(self, pstrFormFieldNa me, pstrFilePathAnd Name):
            > > form = cgi.FieldStorag e()[/color]
            >
            > You are instantiating cgi.FieldStorag e twice. This won't work for POST
            > requests, because instantiating a FieldStorage reads the form data from
            > the standard input stream (the HTTP request).
            >
            > Try to create a second one and cgi will try to read all the form data
            > again; this will hang, waiting for the socket to send it a load more
            > data which will not be forthcoming.
            >
            > When using CGI, parse the input only once, then pass the results (a
            > FieldStorage object if you are using the cgi module) in to any other
            > functions that need to read it.
            >
            > --
            > Andrew Clover
            > mailto:and@doxd esk.com
            > http://www.doxdesk.com/
            >[/color]


            Comment

            • Tim Roberts

              #7
              Re: File Uploads

              "Doug Helm" <dhelm@wcsoftwa re.com> wrote:
              [color=blue]
              >Hey, Folks:
              >
              >I'm trying to write a very simple file upload CGI. I'm on a Windows server.
              >I *am* using the -u switch to start Python for CGIs, as follows:
              >
              >c:\python\pyth on.exe -u %s %s
              >
              >I *do* have write permissions on the directory I'm trying to write to. But,
              >when I click submit, it just hangs. Any help would be greatly appreciated.
              >Thanks. Here's the code...
              >
              >Upload.py
              >
              >import cgi
              >
              >print "content-type: text/html\n\n"[/color]

              I see you got your problem solved, but you should know there is a problem
              with this line. The print statement automatically adds an end-of-line, so
              this will actually end up producing TWO blank lines after the header. You
              should use this:

              print "Content-type: text/html\n"
              --
              - Tim Roberts, timr@probo.com
              Providenza & Boekelheide, Inc.

              Comment

              • Doug Helm

                #8
                Re: File Uploads

                You're right, of course, and I do appreciate it. I generally am calling
                functions and returning strings and then printing the entire string.
                For example:

                def SomeFunc():
                lstrRetVal = ''
                lstrRetVal += 'Content-type: text/html\n\n'
                lstrRetVal += more HTML here...
                return lstrRetVal

                Then, the calling code does:

                print SomeFunc()

                In this case, the extra new line character is appropriate. Somehow, the
                extra new line character slipped in on the print statement in my upload
                sample code (I probably copied from a function that returns a string). But
                thanks just the same...

                Just to be complete (so that no one comments about string concatenation
                efficiency), in a real application I would generally use triple quotes for
                HTML (or append to a list and then .join into a string at the end)...

                Thanks to all for your help.

                "Tim Roberts" <timr@probo.com > wrote in message
                news:n3ok41l4u6 k6sei2ndb03k26m hmh1sv43i@4ax.c om...[color=blue]
                > "Doug Helm" <dhelm@wcsoftwa re.com> wrote:
                >[color=green]
                > >Hey, Folks:
                > >
                > >I'm trying to write a very simple file upload CGI. I'm on a Windows[/color][/color]
                server.[color=blue][color=green]
                > >I *am* using the -u switch to start Python for CGIs, as follows:
                > >
                > >c:\python\pyth on.exe -u %s %s
                > >
                > >I *do* have write permissions on the directory I'm trying to write to.[/color][/color]
                But,[color=blue][color=green]
                > >when I click submit, it just hangs. Any help would be greatly[/color][/color]
                appreciated.[color=blue][color=green]
                > >Thanks. Here's the code...
                > >
                > >Upload.py
                > >
                > >import cgi
                > >
                > >print "content-type: text/html\n\n"[/color]
                >
                > I see you got your problem solved, but you should know there is a problem
                > with this line. The print statement automatically adds an end-of-line, so
                > this will actually end up producing TWO blank lines after the header. You
                > should use this:
                >
                > print "Content-type: text/html\n"
                > --
                > - Tim Roberts, timr@probo.com
                > Providenza & Boekelheide, Inc.[/color]


                Comment

                Working...