cgi: getting at raw POST data?

E

Erik Johnson

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>
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))
 
S

Sarat Venugopal

Erik said:
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?)

Often, people do require abnormal things.
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.

As far as CGI is concerned, the POST data resides in stdin. So accessing it
is as simple as reading sys.stdin within the CGI script. Of course, it is up
to you to interpret the data, though you can just read everything as a
string. The POST data could just as well be binary - such as a file upload.

To dump the strings to a file to your webserver, try the following script:

import sys

print 'Content-type: text/plain\r\n\r\n'

dumped = open('test.txt', 'wb')
for line in sys.stdin.readline():
dumped.write(line)

HTH,
Sarat Venugopal
www.huelix.com
 
M

madsurfer2000

Erik said:
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.

Got this from a quick search with Google. Haven't tried it, but it
looks like it does what you want.

http://mail.python.org/pipermail/python-list/2002-September/122977.html
 
T

Tim Roberts

Erik Johnson said:
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.

If you are using POST in some non-standard way, just skip using the cgi
module at all and read stdin yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top