Uploading images via cgi?

C

Chris Dewin

How do I go about writing a cgi script, that will enable the client to
upload things to a directory on my website?

I would also like to write a script that enables the client to delete
items in that directory if they want. Given that it's unix server, how do
I go about ensuring the files are written to that dir with the right
permissions?
 
P

Peter Hansen

Chris said:
How do I go about writing a cgi script, that will enable the client to
upload things to a directory on my website?

I would also like to write a script that enables the client to delete
items in that directory if they want. Given that it's unix server, how do
I go about ensuring the files are written to that dir with the right
permissions?

What did you learn when you tried a few of the more obvious searches
with Google (including the word "python" in the search)? You must have
learned _something_ but still have questions, so please tell us what you
already know so we can skip over the easy stuff.

Knowing what you've already discovered will avoid having everyone here
repeat all the same answers (which you can easily find in the Google
Groups archives) to what is a fairly common sort of question.

-Peter
 
C

Chris Dewin

What did you learn when you tried a few of the more obvious searches
with Google (including the word "python" in the search)? You must have
learned _something_ but still have questions, so please tell us what you
already know so we can skip over the easy stuff.

Knowing what you've already discovered will avoid having everyone here
repeat all the same answers (which you can easily find in the Google
Groups archives) to what is a fairly common sort of question.

-Peter

Very well then. I did search google, and found a number of python scripts
that already do the job (more or less). But when I try to tweak them to my
specifications, I get errors ... probably because I don't really
understand how the process of uploading a file via a webform to a cgi
works.

For e.g, take the script at this url.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/273844

By reading through this script, I know that simply putting '<input
name="file_1" type="file">' in your web form will establish a connection
to the cgi when the client submits the form. The python interpreter
obviously sustains that connection while the file is being transferred,
the file being part of the FieldStorage() instance. But then, the script
saves the file to the disk in some special way I don't understand. In this
case:
fout = file (os.path.join(upload_dir, fileitem.filename), 'wb') while
1:
chunk = fileitem.file.read(100000)
if not chunk: break
fout.write (chunk)
fout.close()

chunk = fileitem.file.read(100000), and fout.write (chunk) seem to be the
key statements. Could someone elaborate on these for me please? And why
do they need to be in a while loop?
 
D

Dennis Lee Bieber

I'm presuming the "while" was really on the next line with the 1:
chunk = fileitem.file.read(100000), and fout.write (chunk) seem to be the
key statements. Could someone elaborate on these for me please? And why
do they need to be in a while loop?

The read is attempting to read, as the variable says, "chunks" of
the file up to a maximum of 100K(decimal) bytes at a time, then writes
that "chunk" to the local output file. The loop is needed to retrieve
the rest of the file data. When the read returns an empty "chunk", the
file is done..
--
 
P

Paul Boddie

Chris said:
chunk = fileitem.file.read(100000), and fout.write (chunk) seem to be the
key statements. Could someone elaborate on these for me please? And why
do they need to be in a while loop?

I imagine that the intention of reading only 100000 bytes at a time is
to limit the amount of memory used by the process. In an environment
where the process is kept around (not CGI, however) doing so might be
critical since it has been alleged that Python isn't particularly good
at freeing memory back to the operating system.

One way of limiting the total size of uploaded files might be to
examine the "Content-Length" header and to reject (ie. not process)
uploads involving files above a certain size.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top