I would like to talk about the best way in my opinion to develop a website. Simply, I program in Python3 to develop a website.
Python3 continues to improve their software, and they have decided to remove the cgi and cgitb modules, that’s ok, because there are solutions to accomplish receiving POST and GET data from the Internet.
When Bill Gates asked, why hasn’t programming languages become easier, my answer would be Python3 does that.
How to replace cgi module that is depreciated in 3.13 version of Python3?
Option #1
Simply install the pip package from https://pypi.org/project/legacy-cgi
Option #2
Install the pip package multipart from https://github.com/defnull/multipart
Multipart is nice, it seems optimized and nicely coded.
Once multipart is installed, you may need something like the following to get it to work with the cgi of Apache2.
class MyCGI():
"""
should be the following:
forms, files = parse_form_data(os.environ)
its for WSGI but not CGI,
therefore to make for Apache's CGI,
I created this MyCGI() class to GET, POST, retrieve upload data
using Apache2 and mod_cgi, mod_cgid
"""
_os_environ = []
_escape = True
_data = {}
def __init__(self, os_environ, escape=True):
self._os_environ = os_environ
self._escape = escape
def FieldStorage(self):
e = self._os_environ
if 'REQUEST_METHOD' in e:
if e['REQUEST_METHOD'] == 'GET':
self._data = urllib.parse.parse_qs(e['QUERY_STRING'])
elif e['REQUEST_METHOD'] == 'POST':
if e['QUERY_STRING'] != '':
self._data = urllib.parse.parse_qs(e['QUERY_STRING'])
if 'CONTENT_TYPE' in e:
content_type, options = parse_options_header(e['CONTENT_TYPE'])
if 'multipart/form-data' in e['CONTENT_TYPE']:
if 'boundary' in e['CONTENT_TYPE']:
boundary = options.get("boundary", "")
if 'CONTENT_LENGTH' in e:
try:
content_length = int(os.environ.get("CONTENT_LENGTH", "-1"))
except ValueError:
raise MultipartError("Invalid Content-Length header.")
stream = sys.stdin.buffer
try:
for part in MultipartParser(stream, boundary, content_length):
if part.filename or not part.is_buffered():
files[part.name] = part
except Exception as err:
print(f"{type(err).__name__} exception: {err}")
if 'application/x-www-form-urlencoded' in e['CONTENT_TYPE']:
self._data = urllib.parse.parse_qs(str(sys.stdin.read()), keep_blank_values=True)
if e['QUERY_STRING'] != '':
self._data.update(urllib.parse.parse_qs(e['QUERY_STRING']))
def getvalue(self, arg_key, default=''):
if arg_key in self._data:
value = self._data[arg_key]
if isinstance(value, list):
if self._escape == True:
return escape(self._data[arg_key][0])
else:
return self._data[arg_key][0]
else:
if self._escape == True:
return escape(value)
else:
return value
else:
return default
Usage for a POST or GET from a webpage is something like:
usage:
mycgi = MyCGI(os.environ)
mycgi.FieldStorage()
data1_variable = mycgi.getvalue('data1_variable')
Please note that Python3 requires Apache2 with cgi, a2enmod cgi
, Options +ExecCGI and each file needs run permissions.
If you program with Python3 and or develop website with it, I am a fan of you. Send a text message, I like to find out about Python3 projects.
Happy Coding!