Re: cgi module on VMS x86-64
Posted: Fri Jul 19, 2024 2:39 pm
I was finally able to allocate 60 minutes to solve this problem. The following code works with python3.9 on OpenVMS-9.2-2 from Apache via mod_cgi. Enjoy!
Code: Select all
'''
=============================================================
title : apache$common:[scripts]python_cgi_demo_104.py
author : Neil Rieck
notes :
1) no shebang on OpenVMS so call like this:
create file "apache$common:[scripts]python_cgi_demo.com" with these lines:
$ python :== $PYTHON$ROOT:[bin]python.exe
$! caveats:
$! 1) this technique works via Apache-2.4 on OpenVMS-9.2-2 x86_64
$! 2) it fails on OpenVMS-8.4 on Itanium
$ python python_cgi_demo_104.py
2) http://host.domain.com/scripts/python_cgi_demo?a=1&b=2
3) see top of https://docs.python.org/3/library/cgi.html
4) also see: PEP-333 and PEP-3333
history:
ver who when what
--- --- ------ ----------------------------------------------
100 NSR 240610 original effort (DCL variables)
101 NSR 240618 now import my own logical name library
102 NSR 240624 now copy some DCL stuff into os.environ
103 NSR 240712 1. now employ _decc to read symbols + logicals
2. now import cgi_alt (which contains debug)
104 NSR 240719 1. now import cgi (see: note-3 just above)
=============================================================
'''
# import cgi_alt as cgi
import cgi
import os
import sys
import _decc as DECC # OpenVMS only
TITLE = "python_cgi_demo"
def main():
debug = 1
script = sys.argv[0]
if debug >= 2:
print("Status: 200\n"
"content-type: text/html\n\n"
"<html><body><pre>")
print("env: ",os.environ)
# ===========================
errmsg = "initialized"
try:
stuff = {'CONTENT_LENGTH', 'CONTENT_TYPE', 'QUERY_STRING', 'REQUEST_METHOD'}
for label in stuff:
tmp = DECC.getenv(label)
if tmp is not None and tmp != "":
os.environ[label] = tmp
except Exception as e:
errmsg = e
# ===========================
print("Status: 200\n"
"content-type: text/html\n\n"
"<html>\n<head>\n"
f"<title>{TITLE}</title>\n"
"</head>\n<body>\n"
'<form action="/scripts/python_cgi_demo" method="POST">\n'
'A <input type="text" name="a"/> \n'
'B <input type="text" name="b"/> \n'
'<input type="submit" value="submit"/> \n'
'</form>\n')
if debug >= 1:
print(f"<pre>hello from: {script}\n")
print("errmsg : ", errmsg)
print("cur env: ", os.environ)
print("len env: ", len(os.environ))
try:
if os.environ['REQUEST_METHOD'] == "GET":
# This following works with python3.9 OpenVMS-9.2-2 x86,
# but the spec says to call urllib.parse.parse_qsl()
fld = cgi.FieldStorage(keep_blank_values=True)
if len(fld) > 0:
for name in fld:
value = fld[name].value
if debug >= 1:
print(name," = ",value)
if os.environ['REQUEST_METHOD'] == "POST":
# the next call DOES NOT with python3.9 OpenVMS x86
# fld = cgi.FieldStorage(keep_blank_values=True)
# so let's try this:
fp = open("/APACHE$INPUT/", "r")
fld = cgi.parse(fp, os.environ, keep_blank_values=False, strict_parsing=False, separator='&')
print(fld)
if len(fld) > 0:
for name in fld:
value = fld[name]
if type(value) != str:
value = value[0]
if debug >= 1:
print(name," = ",value)
except Exception as e:
if debug >= 1:
print(f"cgi.Field error: {e}")
else:
# insert code here to write to a log file
print(f"<!-- cgi.Field error: {e} -->")
if debug >= 1:
print(os.environ)
for x in os.environ:
y = os.environ[x]
print(f"{x} = {y}")
print("</pre>")
print("</body></html>")
if __name__ == "__main__":
main()
#this is the last line