File Uploads

Discussion in 'Python' started by Doug Helm, Mar 27, 2005.

  1. Doug Helm

    Doug Helm Guest

    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\python.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.FieldStorage()
    if not form:
    print """
    <html>
    <head></head>
    <body>
    <form name="frmMain" action="Upload.py" method="POST"
    enctype="multipart/form-data">
    <input type="file" name="filename"><br>
    <input type="submit">
    </form>
    </body>
    </html>
    """
    else:
    import BLOB
    lobjUp = BLOB.BLOB()
    if lobjUp.Save('filename', '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(staticobject.StaticObject):

    def __init__(self):
    self.initializing = cTrue
    staticobject.StaticObject.__init__(self)
    self.initializing = cFalse

    def Save(self, pstrFormFieldName, pstrFilePathAndName):

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

    form = cgi.FieldStorage()
    f = open(pstrFilePathAndName,'wb')
    f.write(form[pstrFormFieldName].value)
    f.close()
    Doug Helm, Mar 27, 2005
    #1
    1. Advertising

  2. 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 <> wrote:
    > 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\python.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.FieldStorage()
    > if not form:
    > print """
    > <html>
    > <head></head>
    > <body>
    > <form name="frmMain" action="Upload.py" method="POST"
    > enctype="multipart/form-data">
    > <input type="file" name="filename"><br>
    > <input type="submit">
    > </form>
    > </body>
    > </html>
    > """
    > else:
    > import BLOB
    > lobjUp = BLOB.BLOB()
    > if lobjUp.Save('filename', '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(staticobject.StaticObject):
    >
    > def __init__(self):
    > self.initializing = cTrue
    > staticobject.StaticObject.__init__(self)
    > self.initializing = cFalse
    >
    > def Save(self, pstrFormFieldName, pstrFilePathAndName):
    >
    > # tried this first -- same result -- just hangs...
    > # try:
    > # form = cgi.FieldStorage()
    > # item = form[pstrFormFieldName]
    > # if item.file:
    > # data = item.file.read()
    > # f = open(pstrFilePathAndName,'wb')
    > # f.write(data)
    > # f.close()
    > # return cTrue
    > # else:
    > # return cFalse
    > # except:
    > # return cFalse
    >
    > form = cgi.FieldStorage()
    > f = open(pstrFilePathAndName,'wb')
    > f.write(form[pstrFormFieldName].value)
    > f.close()
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    Please visit dimitri's website: www.serpia.com
    dimitri pater, Mar 27, 2005
    #2
    1. Advertising

  3. Doug Helm

    Doug Helm Guest

    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" <> wrote in message
    news:...
    > 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 <>

    wrote:
    > > 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\python.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.FieldStorage()
    > > if not form:
    > > print """
    > > <html>
    > > <head></head>
    > > <body>
    > > <form name="frmMain" action="Upload.py" method="POST"
    > > enctype="multipart/form-data">
    > > <input type="file" name="filename"><br>
    > > <input type="submit">
    > > </form>
    > > </body>
    > > </html>
    > > """
    > > else:
    > > import BLOB
    > > lobjUp = BLOB.BLOB()
    > > if lobjUp.Save('filename', '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(staticobject.StaticObject):
    > >
    > > def __init__(self):
    > > self.initializing = cTrue
    > > staticobject.StaticObject.__init__(self)
    > > self.initializing = cFalse
    > >
    > > def Save(self, pstrFormFieldName, pstrFilePathAndName):
    > >
    > > # tried this first -- same result -- just hangs...
    > > # try:
    > > # form = cgi.FieldStorage()
    > > # item = form[pstrFormFieldName]
    > > # if item.file:
    > > # data = item.file.read()
    > > # f = open(pstrFilePathAndName,'wb')
    > > # f.write(data)
    > > # f.close()
    > > # return cTrue
    > > # else:
    > > # return cFalse
    > > # except:
    > > # return cFalse
    > >
    > > form = cgi.FieldStorage()
    > > f = open(pstrFilePathAndName,'wb')
    > > f.write(form[pstrFormFieldName].value)
    > > f.close()
    > >
    > > --
    > > http://mail.python.org/mailman/listinfo/python-list
    > >

    >
    >
    > --
    > Please visit dimitri's website: www.serpia.com
    Doug Helm, Mar 28, 2005
    #3
  4. 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 <> wrote:
    > 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" <> wrote in message
    > news:...
    > > 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 <>

    > wrote:
    > > > 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\python.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.FieldStorage()
    > > > if not form:
    > > > print """
    > > > <html>
    > > > <head></head>
    > > > <body>
    > > > <form name="frmMain" action="Upload.py" method="POST"
    > > > enctype="multipart/form-data">
    > > > <input type="file" name="filename"><br>
    > > > <input type="submit">
    > > > </form>
    > > > </body>
    > > > </html>
    > > > """
    > > > else:
    > > > import BLOB
    > > > lobjUp = BLOB.BLOB()
    > > > if lobjUp.Save('filename', '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(staticobject.StaticObject):
    > > >
    > > > def __init__(self):
    > > > self.initializing = cTrue
    > > > staticobject.StaticObject.__init__(self)
    > > > self.initializing = cFalse
    > > >
    > > > def Save(self, pstrFormFieldName, pstrFilePathAndName):
    > > >
    > > > # tried this first -- same result -- just hangs...
    > > > # try:
    > > > # form = cgi.FieldStorage()
    > > > # item = form[pstrFormFieldName]
    > > > # if item.file:
    > > > # data = item.file.read()
    > > > # f = open(pstrFilePathAndName,'wb')
    > > > # f.write(data)
    > > > # f.close()
    > > > # return cTrue
    > > > # else:
    > > > # return cFalse
    > > > # except:
    > > > # return cFalse
    > > >
    > > > form = cgi.FieldStorage()
    > > > f = open(pstrFilePathAndName,'wb')
    > > > f.write(form[pstrFormFieldName].value)
    > > > f.close()
    > > >
    > > > --
    > > > http://mail.python.org/mailman/listinfo/python-list
    > > >

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

    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    Please visit dimitri's website: www.serpia.com
    dimitri pater, Mar 28, 2005
    #4
  5. Doug Helm

    Guest

    Doug Helm wrote:

    > form = cgi.FieldStorage()
    > if lobjUp.Save('filename', 'SomeFile.jpg'):


    > class BLOB(staticobject.StaticObject):
    > def Save(self, pstrFormFieldName, pstrFilePathAndName):
    > form = cgi.FieldStorage()


    You are instantiating cgi.FieldStorage 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:
    http://www.doxdesk.com/
    , Mar 28, 2005
    #5
  6. Doug Helm

    Doug Helm Guest

    Andrew:

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

    Doug

    <> wrote in message
    news:...
    > Doug Helm wrote:
    >
    > > form = cgi.FieldStorage()
    > > if lobjUp.Save('filename', 'SomeFile.jpg'):

    >
    > > class BLOB(staticobject.StaticObject):
    > > def Save(self, pstrFormFieldName, pstrFilePathAndName):
    > > form = cgi.FieldStorage()

    >
    > You are instantiating cgi.FieldStorage 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:
    > http://www.doxdesk.com/
    >
    Doug Helm, Mar 29, 2005
    #6
  7. Doug Helm

    Tim Roberts Guest

    "Doug Helm" <> wrote:

    >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\python.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"


    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,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Mar 30, 2005
    #7
  8. Doug Helm

    Doug Helm Guest

    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" <> wrote in message
    news:...
    > "Doug Helm" <> wrote:
    >
    > >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\python.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"

    >
    > 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,
    > Providenza & Boekelheide, Inc.
    Doug Helm, Apr 1, 2005
    #8
    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. Todd Denlinger

    file uploads with progress bar

    Todd Denlinger, Oct 21, 2003, in forum: ASP .Net
    Replies:
    8
    Views:
    667
    Kenneth Keeley
    Oct 24, 2003
  2. Lauchlan M

    filenames / extensions with file uploads

    Lauchlan M, Mar 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    357
    Lauchlan M
    Mar 3, 2004
  3. Ryan Ternier

    Multiple File Uploads

    Ryan Ternier, Jul 7, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    360
    Kevin Spencer
    Jul 8, 2004
  4. Rajeev Tipnis

    Handling Errors in Large File Uploads

    Rajeev Tipnis, Nov 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    415
    Rajeev Tipnis
    Nov 10, 2004
  5. Mufasa
    Replies:
    2
    Views:
    262
    Alexey Smirnov
    Dec 21, 2007
Loading...

Share This Page