cgi: getting at raw POST data?

Discussion in 'Python' started by Erik Johnson, Feb 26, 2005.

  1. Erik Johnson

    Erik Johnson Guest

    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))
     
    Erik Johnson, Feb 26, 2005
    #1
    1. Advertising

  2. Re: getting at raw POST data?

    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
     
    Sarat Venugopal, Feb 26, 2005
    #2
    1. Advertising

  3. Erik Johnson

    Guest

    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/python-list/2002-September/122977.html
     
    , Feb 26, 2005
    #3
  4. Erik Johnson

    Tim Roberts Guest

    "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,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Feb 27, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al Stoltz
    Replies:
    1
    Views:
    1,015
    Andrew Clover
    Jan 23, 2004
  2. Replies:
    5
    Views:
    1,074
  3. Boris Punk
    Replies:
    8
    Views:
    338
    Stefan Ram
    Jun 29, 2010
  4. Carlo Razzeto

    Webservices with raw post data

    Carlo Razzeto, Nov 30, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    493
  5. Jeremy Wall

    access raw CGI post data

    Jeremy Wall, Feb 10, 2004, in forum: Ruby
    Replies:
    1
    Views:
    303
    Eric Schwartz
    Feb 10, 2004
Loading...

Share This Page