raw post data 
Author Message
 raw post data

how would i retrieve the raw post data in cgi python scripts ?

cheers



Fri, 04 Mar 2005 07:12:54 GMT  
 raw post data

Quote:

> how would i retrieve the raw post data in cgi python scripts ?

I think something like:

import sys
all_posted_lines = sys.stdin.readlines()

should do it.  Raw POST'ed data should be no more and no less
than the contents of the CGI script's standard-input.

Alex



Fri, 04 Mar 2005 15:45:32 GMT  
 raw post data
thanks alex

:B


Quote:

> > how would i retrieve the raw post data in cgi python scripts ?

> I think something like:

> import sys
> all_posted_lines = sys.stdin.readlines()

> should do it.  Raw POST'ed data should be no more and no less
> than the contents of the CGI script's standard-input.

> Alex



Fri, 04 Mar 2005 16:22:03 GMT  
 raw post data
how could i make it so when i read the stdin it still is there.. cause i use
cgi.FormVariables() also

cheers


Quote:

> > how would i retrieve the raw post data in cgi python scripts ?

> I think something like:

> import sys
> all_posted_lines = sys.stdin.readlines()

> should do it.  Raw POST'ed data should be no more and no less
> than the contents of the CGI script's standard-input.

> Alex



Sat, 05 Mar 2005 12:01:16 GMT  
 raw post data

Quote:

> how could i make it so when i read the stdin it still is there.. cause i
> use cgi.FormVariables() also

When you instantiate class cgi.FieldStorage (there's no such
thing as cgi.FormVariables, so I'm just guessing at what it IS
that you want!) you can pass it an optional first parameter
that is a file object open for reading -- it uses sys.stdin
as the default if you don't pass the first parameter.

Thus, the following should work (untested!):

import cgi, sys, cStringIO

copyInput = cStringIO.StringIO(sys.stdin.read())
fieldStorage = cgi.FieldStorage(copyInput)

The raw posted data are now available as the string returned
by calling copyInput.getvalue(), and are also parsed by cgi to
obtain fieldStorage.

Alex



Sat, 05 Mar 2005 14:52:08 GMT  
 raw post data
yeah good guess! and thats something that looks like it will werk =B

thanks


Quote:

> > how could i make it so when i read the stdin it still is there.. cause i
> > use cgi.FormVariables() also

> When you instantiate class cgi.FieldStorage (there's no such
> thing as cgi.FormVariables, so I'm just guessing at what it IS
> that you want!) you can pass it an optional first parameter
> that is a file object open for reading -- it uses sys.stdin
> as the default if you don't pass the first parameter.

> Thus, the following should work (untested!):

> import cgi, sys, cStringIO

> copyInput = cStringIO.StringIO(sys.stdin.read())
> fieldStorage = cgi.FieldStorage(copyInput)

> The raw posted data are now available as the string returned
> by calling copyInput.getvalue(), and are also parsed by cgi to
> obtain fieldStorage.

> Alex



Sat, 05 Mar 2005 20:11:55 GMT  
 raw post data

Quote:


>> how could i make it so when i read the stdin it still is there.. cause i
>> use cgi.FormVariables() also

>When you instantiate class cgi.FieldStorage (there's no such
>thing as cgi.FormVariables, so I'm just guessing at what it IS
>that you want!) you can pass it an optional first parameter
>that is a file object open for reading -- it uses sys.stdin
>as the default if you don't pass the first parameter.

>Thus, the following should work (untested!):

>import cgi, sys, cStringIO

>copyInput = cStringIO.StringIO(sys.stdin.read())
>fieldStorage = cgi.FieldStorage(copyInput)

>The raw posted data are now available as the string returned
>by calling copyInput.getvalue(), and are also parsed by cgi to
>obtain fieldStorage.

A nit, but it might be good to check that the environment
variable REQUEST_METHOD exists and == 'POST',
and to attempt to read not more than the most you expect to be able to handle,
and to check that what you get with your max read is what it's supposed to be,
e.g., (untested!) (you specify MAX_I_HANDLE) (inside appropriate try/except)

    import os
    ...
    if os.environ.get('REQUEST_METHOD') is None or os.environ.get('REQUEST_METHOD') != 'POST':
        raise ValueError, 'Request method not POST'
    if os.environ.get('CONTENT_LENGTH') is None: raise ValueError, 'Content length unspecified'
    content_length = int(os.environ.get('CONTENT_LENGTH'))
    if content_length == 0: raise ValueError, 'Content length zero'
    elif content_length > MAX_I_HANDLE: raise valueError, 'Content length too great: %s' % content_length
    copyInput = cStringIO.StringIO(sys.stdin.read(MAX_I_HANDLE))
    if len(copyInput) != content_length:
        raise IOError, 'Data bytes read (%s) not == CONTENT_LENGTH (%s)' % (len(copyInput), content_length)
    ...

Regards,
Bengt Richter



Sun, 06 Mar 2005 02:56:50 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Extract data from radius's raw data

2. PyObject *data - access to raw data?

3. Form posting problem and post data issue

4. Raw data from the Aus Stocks

5. Outputting 'Raw' printer data

6. Eiffel and "raw" data storage

7. USPS/UPS/etc shipping rate raw data...

8. I just want the raw data...

9. newbie:displaying raw binary data

10. Variable length raw-byte data

11. Attempting to read variable length records as raw data

12. Using DEC Fortran to read raw binary data?

 

 
Powered by phpBB® Forum Software