simple cgi program
superpollo
utente at esempio.net
Sun Jan 17 09:15:12 EST 2010
hi clp.
i would like to submit the following code for review. it is a simple
common gateway interface program, which uses the least possible
libraries for the sake of mechanism undertanding.
the third option in the firts conditional is for commandline testing.
bye.
====
code follows as:
#!/usr/bin/env python
import sys
import os
import urllib as url
me = os.path.basename(sys.argv[0])
env = os.environ
method = "POST"
data=None
if env.get("REQUEST_METHOD") == "GET":
data = env.get("QUERY_STRING")
elif env.get("REQUEST_METHOD") == "POST":
data = sys.stdin.read(int(env.get("CONTENT_LENGTH")))
elif not env.get("REQUEST_METHOD"):
data = sys.stdin.readline().rstrip("\n")
if not data:
sys.stdout.write("Content-type: text/html\n\n")
sys.stdout.write("<FORM ACTION='%s' METHOD='%s'>\n" % (me , method))
sys.stdout.write("TYPE A DIGIT SEQUENCE AND HIT [ENTER]:\n")
sys.stdout.write("<INPUT NAME='digits' SIZE='20'>\n")
sys.stdout.write("</FORM>\n")
else:
name = data.partition("=")[0]
encvalue = data.partition("=")[2]
decvalue = url.unquote_plus(encvalue)
sys.stdout.write("Content-type: text/plain\n\n")
sys.stdout.write("method: %s\n" % method)
sys.stdout.write("data: %s\n" % data)
sys.stdout.write("name: %s\n" % name)
sys.stdout.write("encvalue: %s\n" % encvalue)
sys.stdout.write("decvalue: %s\n" % decvalue)
if decvalue.isdigit():
sys.stdout.write("the sequence you typed is: %s\n" % decvalue)
else:
sys.stdout.write("what you typed is not a valid sequence\n")
====
criticism and advice are appreciated!
More information about the Python-list
mailing list