File upload script

Steinar Dragsnes st03210 at
Tue Sep 17 08:26:38 EDT 2002


I'm a complete newbe in python and have never written anything more
advanced then the classical Hallo World example. I have already
programmed an applet where users should be able to upload images and or
other files from their local pc. This is of course not possible due to
the applet security model (sandbox) in Java. But I can start a
python-script from the running java applet which can do the actual file
upload for me and save the file to a specific directory on the apache

Since the applet that needs the information about what file has been
uploaded into what node, I need a way to send back information to the
applet calling the script. I wonder if this is possible at all? Usually
scripts generate html-pages and call applets from there on they fly,
with the right paranemer tag values. Is this possible when the applet is
already running? Would it not destroy the applet context?

If this is the case, a workaround could be to write the output from the
script to a text-file. The information I need to write is sessionID,
userID, nodeID and file name. This is a "hairy" solution, but the most
important thing for me is a solution that works right now.

My main problem is that I do not know how I can pass the parameters of
type String; sessionID, userID and nodeID, from the appleten to the
called python-script. I need to know how to write a function that will
receive these parameters. The python script below is written by a
colleague of mine, and he is now on a sick-leave....and I need to figure
this out soon....

In the java code I call the script like this: url=new"http://"+ip+"/cgi-bin/");

Below is the script that I need to rewrite so that it can take the
parameters I mentioned above:
Thanks in advance,
Steinar Dragsnes.


import cgi
form = cgi.FieldStorage()
print 'Content-type: text/html'

if not form:
        print """
        <html><head><title>File Upload</title></head><body>
        <form action"/cgi-bin/" method="POST"
        <input type="file" name="fila">
        <input type="submit" name="Send_File" value="Upload">
        Upload an image.
elif form.has_key("fila"):
        item = form["fila"]
        if item.file:
                data =
                filnavn = item.filename
                filen1 = filnavn.split('\\')
                filen2 = filen1[len(filen1)-1]
                #filen2 = 'd.txt'
                open('/var/www/html/bilete/' + filen2,'wb').write(data)
                print """
                <html><head><title>File Upload</title></head><body>
                File has been uploaded.
                print """
                <applet code=UploadedFile.class

                width="200" height="200">
                <PARAM NAME=filNavn VALUE=
                print cgi.escape(filen2)
                print """
                print cgi.escape(filen2)
                print '</body></html>'


More information about the Python-list mailing list