File upload script

Steinar Dragsnes st03210 at student.uib.no
Tue Sep 17 08:26:38 EDT 2002


Hi!

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
server.

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 upload.py script like this:
     java.net.URL url=new
java.net.URL("http://"+ip+"/cgi-bin/upload.py");
    applet.getAppletContext().showDocument(url,"_blank");

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

---------------------------
#!/usr/bin/python

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

if not form:
        print """
        <html><head><title>File Upload</title></head><body>
        <form action"/cgi-bin/upload.py" method="POST"
enctype="multipart/form-data">
        <input type="file" name="fila">
        <input type="submit" name="Send_File" value="Upload">
        </form>
        <p>
        Upload an image.
        </p>
        </body></html>
        """
elif form.has_key("fila"):
        item = form["fila"]
        if item.file:
                data = item.file.read()
                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 """
                <p>
                <applet code=UploadedFile.class
                codebase="http://tordenskrall.intermedia.uib.no/applets"

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

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






More information about the Python-list mailing list