On Mon, May 05, 2003 at 05:06:44PM +0200, Johann Spies wrote: > Writing cgi's is new to me. > > I am trying to write a cgi to upload a file and after a search on > google found an example written in python by Guido von Rossum. > > I thought I would take that as my basis but so far I have had only > frustration. > > The form: > > ----------------------------------- > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> > <HTML> > <HEAD> > <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> > </HEAD> > > <form action="http://js/cgi-bin/fupload.cgi" method="POST" enctype="multipart/form-data"> > <input type="file" name="filename"> > <input type="submit"> > </form> > </BODY> > </HTML> > --------------------------- > > "js" is a VirtualHost on the machine. Suexec is active and other > cgi's (for testing purposes) work correctly. > > fupload.cgi looks like this: > -------------------------- > #!/usr/bin/python > import cgi > print "Content-type: text/html\r\n\r\n" > form = cgi.FieldStorage() > if not form: > print """ > <form action="/cgi-bin/test.py" method="POST" enctype="multipart/form-data"> Well, the target of the form is wrong here (you called the form fupload.cgi, but the target is test.py). > <input type="file" name="filename"> > <input type="submit"> > </form> > """ > elif form.has_key("filename"): > item = form["filename"] > if item.file: > output=open ("/var/www/js/cgi-bin/tmp/%s" % item, 'w') > data = item.file.read() # read contents of file > ouput.write (data) > --------------------------- > I actually can't get this script to work on my machine, I get an error that output is undefined at the output.write One trick that helps in debugging CGIs is to set sys.stderr = sys.stdout This will cause errors to be sent to the browser instead of the logs. (you must import sys to do this. The changed top of the script would be: #!/usr/bin/python import sys import cgi sys.stderr = sys.stdout print "Content-type: text/html\r\n\r\n" form = cgi.FieldStorage() ... ) But what really needs to happen is that you need to look at your apache config file (/etc/apache/httpd.conf in a standard debian install) and see what's going on with your logs. It's possible that the /var/www/js/ virtual host has its own log files, in which case that's where your errors should be appearing. I say that it is more important to look at the logs because setting stderr to stdout is only useful after the script acutally starts. If there is some problem that causes the web server to not be able to feed *any* output from the script to the browser, you won't get off the ground at all. The logs will tell you where to look first. Two questions that are common problems: is python actually /usr/bin/python? (it should be on a standard debian) is the script executable? ben@orange /usr/lib/cgi-bin % ls -al fupload.cgi -rwxr-xr-x 1 ben ben 563 May 7 09:56 fupload.cgi* These are the two most common things I forget to check (python often lives in different places on different distributions / operating systems. ::sigh::). -ben -- Ben Hartshorne email: ben@hartshorne.net http://ben.hartshorne.net
Attachment:
pgp0_ALODp9Rj.pgp
Description: PGP signature