Python’s flexible nature means it can bend to almost any application you can imagine and web development is no exception. This article covers simple form handling and creating cookies and presents an example using everything demonstrated.
We first need to make sure that the field 'name' provided is in our 'form'. As long as it is then we get the path (as it appeared in the file field) of the file we want to upload and get the files name using os.path.basename(). We then join it to the 'path' using the os.path.join() function... this lets us to upload files to different places and keep the original filename.
Next we loop over the available file types in 'args' and check the files extension against each type. If the file is a valid type, then we make sure the file doesn't already exists (files won't be uploaded if the file exists to prevent overwriting) before writing the file to the server.
As in the limits() example uploads() can be used with an if statement to check if the function was successful or not. Or even use it with limits() so the user can only upload a set number of files per day.
Ok, by now you should have a fair idea of what exactly writing web applications with python is about and why you would want to, as well as have some idea of what's possible... but this is only the tip of the iceberg!
Hope you've had fun. If you want to learn more about Python or the subjects covered here, then start clicking:
Note: All the sample programs shown and discussed in this article were tested on Windows XP running Python 2.3 under Apache 1.3. Upload script was tested on remote Linux server.