PLEASE Help with CGI Problem!

Gerhard Häring gerhard.nospam at bigfoot.de
Tue May 1 18:14:08 EDT 2001


Just for the fun of it, I did direct translation into Python (not that it will
help you with your problems, see below for why).

On Tue, 01 May 2001 12:56:34 -0700, Ben Ocean <zope at thewebsons.com> wrote:
>Hi;
>I NEED to get data from the buffer after it is sent to me from a foreign 
>host. Here's (apparently) how this would be scripted in perl:
> >>>
>read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
>@pairs = split(/&/, $buffer);
>foreach $pair (@pairs) {
>     ($name, $value) = split(/=/, $pair);
>     $value =~ tr/+/ /;
>     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
>     $FORM{$name} = $value;
>}
><<<
>How would I script it in Python and what modules must I call?

#!/usr/bin/env python
import sys, os, urllib

buffer = sys.stdin.read( int( os.environ[ 'CONTENT_LENGTH' ] ) )
pairs = buffer.split( '&' )
FORM = {}
for pair in pairs:
    ( name, value ) = pair.split( '=' )
    value = value.replace( '+', ' ' )
    value = urllib.unquote( value )
    FORM[ name ] = value

Btw., code not entirely unlike the above can be found in the parse_qsl function
in the cgi module.

As Fredrik already suggested, it might be wise for you at this point to invest
a few hours reading on HTTP and CGI. You will find good ressources when you
search in a directory like dmoz.org. Without understanding what's really going
on, you will for sure stumble across really simple problems a few more times.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de
web:    http://highqualdev.com



More information about the Python-list mailing list