cgi: getting at raw POST data?

Erik Johnson nobodyatanywheredotcom
Fri Feb 25 19:05:27 EST 2005


I am trying to work with a program that is trying make an HTTP POST of text
data without any named form parameter. (I don't know - is that a normal
thing to do?) I need to write a CGI program that accepts and processes that
data. I'm not seeing how to get at data that's not a named form parameter.

    I wrote a simple CGI program to echo a string representation of  the
cgi.FieldStorage class that's recevied, and one to make an HTTP POST to it
(once with the way I normally encode a "standard" HTML form, and once with
just a multiline string). It doesn't look to me like the FieldStorage class
is exposing anything for me to grab ahold of in the latter case. I don't see
any other likely looking candidates in the cgi module. My program code is
below.  Anyone have any suggestions about how to get at raw POST data (as a
CGI program)?

Thanks for taking the time to read my article! :)

-ej


results of running 'post' each way:

> post form
<html>
  <body>
    <pre>FieldStorage(None, None, [MiniFieldStorage('buckle', 'my shoe'),
MiniFieldStorage('one', 'two')])</pre>
  <body>
<html>


> post other
<html>
  <body>
    <pre>FieldStorage(None, None, [])</pre>
  <body>
<html>


#! /usr/local/bin/python
"post"

import os, sys, time, string
import httplib, urllib

what = sys.argv[1]

server = "localhost"
url    = "/test_POST"
conn   = httplib.HTTPConnection(server)
conn.putrequest('POST', url)

if what == 'form':
    form = { 'one' : 'two', 'buckle' : 'my shoe' }
    DATA = urllib.urlencode(form)
else:
    DATA = """\
line 1
this is my little stream of raw data put into POST without
any parameter name.
"""

conn.putheader('Content-Length', str(len(DATA)) )
conn.endheaders()
conn.send(DATA)

response = conn.getresponse()
text = response.read()
conn.close() # done with current machine

print text



#! /usr/bin/python
"test_POST"

# Python STANDARD INCLUDES
import sys
import cgi
import cgitb; cgitb.enable()


# MAIN
# get stuff out of POST parameters
if __name__ == "__main__":


    # dump out a web page
    print """\
Content-type: text/html

<html>
  <body>
    <pre>%s</pre>
  <body>
<html>
""" % str(cgi.FieldStorage(keep_blank_values=True))








More information about the Python-list mailing list