file upload using msvcrt
dxterra
wanner.dx at mellon.com
Wed Sep 11 08:57:18 EDT 2002
I am trying to upload msword docs to my webserver. Here is the code
that I am using:
#!I:\Python22\Python -u
# $Header: $
# $Log: $
import cgi, sys, string, time, types, os
import posixpath, dospath, macpath
import msvcrt
msvcrt.setmode(0, os.O_BINARY)
msvcrt.setmode(1,os.O_BINARY)
from pso.service import ServiceHandler
# allows for more verbose feedback when a cgi exception occurs
import cgitb
cgitb.enable()
sys.stderr = sys.stdout
UPLOAD_DIR = "o:\\upload"
def splitpath( orig_path ):
for pathmodule in [posixpath, dospath, macpath]:
basename = pathmodule.split(orig_path)[1]
if basename != orig_path:
return basename
return orig_path
def save_file(fileinfo):
basename = splitpath( fileinfo.filename)
srvrname = os.path.join(UPLOAD_DIR, basename )
msvcrt.setmode(0, os.O_BINARY)
msvcrt.setmode(1,os.O_BINARY)
fp = file( "s:\\cgi-bin\\project\\log.lis", "a+" )
fp.write( "saving file '%s' as '%s'\n" % (fileinfo.filename,
srvrname) )
fp.write( "file contents %s\n" % fileinfo.value )
fp.write( "file size as read from stdin '%s'\n" % (
len(fileinfo.value) ) )
fp.write( "file mode in '%s' file mode out '%s'\n" %
(sys.stdin.mode, sys.stdout.mode ) )
fp.close()
srvrfile = file(srvrname, "wb" )
while 1:
data = fileinfo.file.read( )
if not data: break
srvrfile.write(data)
return fileinfo.filename
def upload_file():
form = cgi.FieldStorage()
msgs = []
fileinfo = None
for field in form.keys():
if field == "zzz":
if not form[field].filename:
msgs.append( "ERROR: filename is missing" )
else:
fileinfo = form[field]
fp = file( "s:\\cgi-bin\\project\\log.lis", "a+" )
fp.write( "attachment filename is '%s'\n" % (fileinfo.filename) )
fp.close()
if fileinfo is not None:
fp = file( "s:\\cgi-bin\\project\\log.lis", "a+" )
fp.write( "preparing to save attachment '%s'\n" %
(fileinfo.filename) )
fp.close()
srvrname = save_file(fileinfo)
# process the form data
for msg in msgs:
print '<center><font class="RedBold">%s</font></center>' % (msg)
if __name__ == '__main__':
upload_file()
I cannot get the binary file to write. It get to about 8k and then
stops.
When I query the mode it always says 'r' for stdin and 'w' for stdout.
Can anyone help me?
More information about the Python-list
mailing list