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.
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.
This will dump the information.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>")
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.
Aaah, I see. Thanks a lot![]()
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.