+ Reply to Thread
Results 1 to 4 of 4

Thread: Python - GET and POST?

  1. #1
    bazzie is offline x10Hosting Member bazzie is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    2

    Python - GET and POST?

    Yes, I'm someone who actually got python working :P

    So, how do I get data via GET or POST? The latter is not absolutely necessary but would be nice to know too. I've only sys.argv so far, but that only returns the filename.

    Thanks in advance.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Python - GET and POST?

    Code:
    #!/usr/bin/env python
    import os
    import sys
    
    
    print("Content-type: text/html")
    print("")
    
    print("<html>")
    print("<body>")
    
    print( "GET / POST Information")
    print("<pre>")
    
    print "Request method: " , os.environ[ 'REQUEST_METHOD' ]
    print "Query string: " ,  os.environ[ 'QUERY_STRING' ]
    count = 0 
    for line in sys.stdin:
        count = count + 1
        print line
        if count > 100:
            break
    print ""
    
    print("</pre>")
    print("</body>")
    print("</html>")
    This will dump the information.
    QUERY_STRING is mostly for GET (a POST can have a query string too)
    sys.stdin gives you the data from the POST
    You then have to parse the info, splitting first on '&' to separate the fields, then on '=' to make key-value pairs.
    Then you would have to un-urlencode them too.
    That is the low-level way to do it. I would assume there is a CGI module that would be easier to use.
    Last edited by descalzo; 04-22-2010 at 06:12 PM.
    Nothing is always absolutely so.

  3. #3
    bazzie is offline x10Hosting Member bazzie is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    2

    Thumbs up Re: Python - GET and POST?

    Aaah, I see. Thanks a lot

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python - GET and POST?

    Quote Originally Posted by descalzo View Post
    That is the low-level way to do it. I would assume there is a CGI module that would be easier to use.
    Indeed there is.
    Code:
    #!/usr/bin/env python
    
    import cgi, cgitb
    cgitb.enable()
    
    print "Content-type: text/html"
    print
    print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
    print '<html><head><title>Python CGI</title></head><body>'
    
    form = cgi.FieldStorage()
    
    print '<h3>Headers</h3><dl>'
    for header in form.headers:
        print '<dt>%s</dt><dd>%s</dd>' % (header, form.headers[header])
    print '</dl>'
    
    print '<h3>Input</h3><dl>'
    for name in form:
        print '<dt>%s</dt><dd>%s</dd>' % (name, form.getvalue(name))
        # or:
        #print '<dt>%s</dt><dd>%s</dd>' % (name, form[name].value)
    print '</dl>'
    
    cgi.print_form(form)
    
    print '</body></html>'
    Last edited by misson; 04-22-2010 at 07:31 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. [OFF] [35 credits + 5 per post] Register on forum and post
    By hamsn in forum The Marketplace
    Replies: 11
    Last Post: 01-02-2009, 03:51 AM
  2. Replies: 6
    Last Post: 03-27-2005, 12:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers