Home arrow Python arrow Page 5 - Python on the Web

A Last Example - Python

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.

TABLE OF CONTENTS:
  1. Python on the Web
  2. Creating a Warning
  3. Cookies
  4. Sending Email
  5. A Last Example
  6. Inside uploads()...
By: Mark Lee Smith
Rating: starstarstarstarstar / 30
May 18, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

So far you've seen how you can use Python to get forms data, create cookies and send email... in this last example we'll be using as much of what you've learned here as we can (without going over the top).

#!/usr/bin/env python

import cgi, os, sys

sys.stderr = sys.stdout

def uploads(form, name, path, *args):
 if form.has_key(name):
  
  #If the form field exists if 'form' then parse the filename to point
  #at the desired location.
  
  path = os.path.join(path, os.path.basename(form[name].filename))

  for each in args:
   
   #Loop over any available file types and check if the file being
   # uploaded is the right format. Checks if the file already exists
   
   if path.endswith(each) and not os.path.isfile(path):
    file(path, 'wb').write(str(form[name].value))
    
    #Return True to indicate the file was uploaded successfully.
    return True
 
if __name__ == '__main__':
 
 form = cgi.FieldStorage()
 
 print 'Content-Type: text/htmln'
 
 if uploads(form, 'upload', '', '.txt', '.zip'):
  
  #If the upload was successful then print a message.
  
  print 'Finished uploading file...'
 else:
  print 'Failed to upload file. Please visit our help center at...'

If you want to give this a go, you'll need a form set up for file upload; something like this one...

<form name="upload" method="POST" action="upload.py" enctype="multipart/form-data">
  <input name="upload" type="file" /><input type="submit" name="submit" />
</form>

This is pretty small as functions go, but there's quite a lot going on right from the beginning!

As in our other examples, this starts by importing the modules we need into the programs global namespace. Unlike these examples, our next line redirects errors to standard output; this simply sends error messages, as you would get from Python normally to the web browser instead of the error log.

If you're going to use Python for CGI, then you should definitely take a look at the 'cgitb' module at http://www.python.org/doc/2.2.3/lib/module-cgitb.html



 
 
>>> More Python Articles          >>> More By Mark Lee Smith
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: