Way to distinguish between POST and GET data in Python CGIs?

David M. Cooke cookedm+news at physics.mcmaster.ca
Thu Aug 28 06:25:45 CEST 2003


At some point, Simon Willison <cs1spw at bath.ac.uk> wrote:

> Hi all,
>
> I've been experimenting with Python CGIs, and more recently
> mod_python. The FieldStorage class used by Python's cgi module and
> mod_python's utility library allows form data sent by a user's browser
> to be easily accessed, but doesn't appear to provide any method of
> distinguishing between data sent by POST and data sent by GET.

standard CGI variables: the environment variable REQUEST_METHOD is set
to the method (GET or POST). This should do the trick:

if os.environ.get('REQUEST_METHOD', 'GET').upper() != 'POST':
   print 'This script must be accessed using a POST method'
else:
   # do stuff
   form = cgi.FieldStorage()

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca




More information about the Python-list mailing list