Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > File Uploads

Reply
Thread Tools

File Uploads

 
 
Doug Helm
Guest
Posts: n/a
 
      03-27-2005
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()


 
Reply With Quote
 
 
 
 
dimitri pater
Guest
Posts: n/a
 
      03-27-2005
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
 
Reply With Quote
 
 
 
 
Doug Helm
Guest
Posts: n/a
 
      03-27-2005
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:mailman.924.1111960549.1799.python-...
> 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



 
Reply With Quote
 
dimitri pater
Guest
Posts: n/a
 
      03-27-2005
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:mailman.924.1111960549.1799.python-...
> > 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
 
Reply With Quote
 
and-google@doxdesk.com
Guest
Posts: n/a
 
      03-28-2005
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
private.php?do=newpm&u=
http://www.doxdesk.com/

 
Reply With Quote
 
Doug Helm
Guest
Posts: n/a
 
      03-29-2005
Andrew:

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

Doug

<and-> wrote in message
news: ups.com...
> 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
> private.php?do=newpm&u=
> http://www.doxdesk.com/
>



 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      03-30-2005
"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.
 
Reply With Quote
 
Doug Helm
Guest
Posts: n/a
 
      04-01-2005
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.



 
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
Somebody uploads a file - put a button on to preview the file. Mufasa ASP .Net 2 12-21-2007 07:32 PM
Handling Errors in Large File Uploads Rajeev Tipnis ASP .Net 2 11-10-2004 05:20 PM
Multiple File Uploads Ryan Ternier ASP .Net 3 07-08-2004 02:35 PM
filenames / extensions with file uploads Lauchlan M ASP .Net 3 03-03-2004 07:36 AM
file uploads with progress bar Todd Denlinger ASP .Net 8 10-24-2003 05:34 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57