By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

cgi: getting at raw POST data?

P: n/a
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))

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Erik Johnson wrote:
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


Jul 18 '05 #2

P: n/a
Erik Johnson wrote:
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/pyt...er/122977.html

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.