Wnt2bsleepin
04-24-2011, 02:12 PM
Hello, for my entry comp sci class I have to make a python script to something useful. I decided to have a script pull weather information from weather stations.
In order to do this, I must have my script be executable from a browser and thus must use cgi-bin. I have been experimenting with the code to get it to work on my own server. I don't know if it is executing correctly. Here is the output of the page.
Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23)
[GCC 4.4.5]
if I direct my browser to http://webdomain.com/cgi-bin/script.py
Is this a good result? I haven't set up cgi-wrap yet, so I am not too sure if it is necessary.
Here is the entire code contained in script.py
#!/usr/bin/python3
def getargs( qstring ):
rvalue = {}
ipos = 0
prevpos = 0
while ipos < len(qstring):
if qstring[ipos] == '=':
kval = qstring[prevpos:ipos]
ipos = ipos+1
prevpos = ipos
while ipos < len(qstring):
if qstring[ipos] == '&':
vval = qstring[prevpos:ipos]
ipos = ipos+1
prevpos = ipos
break
ipos = ipos+1
if ipos == len(qstring):
vval = qstring[prevpos:ipos]
break
rvalue[kval] = vval
else:
ipos = ipos+1
if ipos == len(qstring):
rvalue["(nokey)"] = qstring[prevpos:ipos]
return rvalue
print ("Content-type: text/html")
print ()
print ("<html>")
print ("<head>")
print ("<title>pystat.py</title>")
print ("</head>")
print ("<body>")
print ("<pre>")
import os, sys
from cgi import escape
print ("<strong>Python %s</strong>" % sys.version)
keys = os.environ.keys()
keys.sort()
for k in keys:
print ("%s\t%s" % (escape(k), escape(os.environ[k])))
print ("</pre>")
print ("<p>")
print ("<h3>",getargs(os.environ["QUERY_STRING"]),"'s Story</h3>")
myargs=getargs(os.environ["QUERY_STRING"])
char1=myargs["arg1"]
char2=myargs["arg2"]
print ("<p>")
print (char1, "and ", char2, "went up the hill to fetch a pail of water.")
print ("</body>")
print ("</html>")
I truly would appreciate any help I could receive.
In order to do this, I must have my script be executable from a browser and thus must use cgi-bin. I have been experimenting with the code to get it to work on my own server. I don't know if it is executing correctly. Here is the output of the page.
Python 3.1.2 (release31-maint, Sep 17 2010, 20:34:23)
[GCC 4.4.5]
if I direct my browser to http://webdomain.com/cgi-bin/script.py
Is this a good result? I haven't set up cgi-wrap yet, so I am not too sure if it is necessary.
Here is the entire code contained in script.py
#!/usr/bin/python3
def getargs( qstring ):
rvalue = {}
ipos = 0
prevpos = 0
while ipos < len(qstring):
if qstring[ipos] == '=':
kval = qstring[prevpos:ipos]
ipos = ipos+1
prevpos = ipos
while ipos < len(qstring):
if qstring[ipos] == '&':
vval = qstring[prevpos:ipos]
ipos = ipos+1
prevpos = ipos
break
ipos = ipos+1
if ipos == len(qstring):
vval = qstring[prevpos:ipos]
break
rvalue[kval] = vval
else:
ipos = ipos+1
if ipos == len(qstring):
rvalue["(nokey)"] = qstring[prevpos:ipos]
return rvalue
print ("Content-type: text/html")
print ()
print ("<html>")
print ("<head>")
print ("<title>pystat.py</title>")
print ("</head>")
print ("<body>")
print ("<pre>")
import os, sys
from cgi import escape
print ("<strong>Python %s</strong>" % sys.version)
keys = os.environ.keys()
keys.sort()
for k in keys:
print ("%s\t%s" % (escape(k), escape(os.environ[k])))
print ("</pre>")
print ("<p>")
print ("<h3>",getargs(os.environ["QUERY_STRING"]),"'s Story</h3>")
myargs=getargs(os.environ["QUERY_STRING"])
char1=myargs["arg1"]
char2=myargs["arg2"]
print ("<p>")
print (char1, "and ", char2, "went up the hill to fetch a pail of water.")
print ("</body>")
print ("</html>")
I truly would appreciate any help I could receive.