Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > cgi: getting at raw POST data?

Reply
Thread Tools

cgi: getting at raw POST data?

 
 
Erik Johnson
Guest
Posts: n/a
 
      02-26-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))





 
Reply With Quote
 
 
 
 
Sarat Venugopal
Guest
Posts: n/a
 
      02-26-2005
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






 
Reply With Quote
 
 
 
 
madsurfer2000@hotmail.com
Guest
Posts: n/a
 
      02-26-2005
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

 
Reply With Quote
 
Tim Roberts
Guest
Posts: n/a
 
      02-27-2005
"Erik Johnson" <nobody at anywhere dot com> 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.


If you are using POST in some non-standard way, just skip using the cgi
module at all and read stdin yourself.
--
- Tim Roberts, http://www.velocityreviews.com/forums/(E-Mail Removed)
Providenza & Boekelheide, Inc.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Maxxum 7D] Dimage RAW Viewer v. Adobe Raw Plugin Alan Browne Digital Photography 3 03-12-2005 04:17 AM
Post-Processing RAW vs Post-Processing TIFF Mike Henley Digital Photography 42 01-30-2005 08:26 AM
Adobe PS RAW convertor vs Nikon RAW convertor Vlad Gunko Digital Photography 8 01-25-2005 07:43 PM
How raw is RAW format? Editor www.nutritionsoftware.org Digital Photography 4 12-22-2003 07:33 PM
Post post post. Shel-hed Computer Support 2 11-08-2003 07:41 AM



Advertisments