1

I'm pretty new to Python and I'm now trying out some examples with Python and HTML.

So it seems that the cgi module will be removed in Python 3.13 (I'm using 3.11 at the moment). Can you folks advise on the simplest Python alternative, without using the cgi.FieldStorage object, to retrieve and process the form data that I get from a html document?

I have included below a simplified example of what I wish to do, i.e. have the user enter his name in 'main.html', have him click the 'Submit' button, which then directs him to the 'result.py' script which shows his name.

main.html

<!DOCTYPE HTML>
<html lang="en">

<head>
<meta charset="UTF8">
<title>Test Program</title>
</head>

<body>

<form method="POST" action="result.py">
Please enter your name: <input type="text" name="name">
<p><input type="submit" value="Submit"></p>
</form>

</body>
</html>

result.py

import cgi

print('Content-type:text/html\r\n\r\n')

print('<!DOCTYPE HTML>')
print('<html lang="en">')

print('<head>')
print('<meta charset="UTF8">')
print('<title>Test Program</title>')
print('</head>')

print('<body>')

data = cgi.FieldStorage()
name = data.getvalue('name')

print(f'<p>Hello, {name}</p>')

# Hyperlink to go back to the main page
print('<a href="main.html">Back</a>')
   
print('</body>')
print('</html>')

So my exact question is, what do I need to change, in order to do the same thing, but without using cgi.FieldStorage?

I've done a bit of searching online, but most of the queries on alternatives to cgi.FieldStorage seem to pertain to file uploads, so I'm not able to get the exact answer I'm hoping for. Any help would be much appreciated!

EDIT to include slightly modified code to achieve the same results but without using cgi.FieldStorage:

main.html (revised)

<!DOCTYPE HTML>
<html lang="en">

<head>
<meta charset="UTF8">
<title>Test Program</title>
</head>

<body>

<form method="GET" action="result.py">
Please enter your name: <input type="text" name="name">
<p><input type="submit" value="Submit"></p>
</form>

</body>
</html>

result.py (revised)

import os
import urllib.parse as ulp

print('Content-type:text/html\r\n\r\n')

print('<!DOCTYPE HTML>')
print('<html lang="en">')

print('<head>')
print('<meta charset="UTF8">')
print('<title>Test Program</title>')
print('</head>')

print('<body>')

# Get the current URL which includes the Query String
url = os.environ["REQUEST_URI"]

# Parse the URL into its subcomponents
parsed = ulp.urlparse(url)

# Parse the Query String portion of the URL;
# this returns a dictionary of 'key:value' pairs,
# with 'key' being the name of the input in the HTML form,
# and 'value' being a list which contains what was
# entered by the user as that input in the same HTML form
dic = ulp.parse_qs(parsed.query, keep_blank_values=True)

# Find the value of the name key in the dictionary;
# dic['name'] returns a list so we append [0] in order
# to access the 1st element of that list.
name = dic['name'][0]

print(f'<p>Hello, {name}</p>')

# Hyperlink to go back to the main page
print('<a href="main.html">Back</a>')

print('</body>')
print('</html>')

I found this article on parsing query strings to be very helpful and have adapted some of the code above: https://bobbyhadz.com/blog/python-parse-url-query-string

It looks like I'd need to eventually look into options like Flask and FastAPI to do what I wanted to do without using cgi and without using a query string, but the edited code above is a simple fix for now that I hope can perhaps help other beginner Python programmers like myself. I used the GET method in the HTML to generate a Query String which includes the value that I want to access, and then use the urlparse and parse_qs methods in Python to access that value.

prog_
  • 11
  • 3
  • Relevant [PEP-594](https://peps.python.org/pep-0594/#cgi) – snakecharmerb Jun 04 '23 at 08:35
  • Rather than writing a CGI script, it might be easier to create a simple web application (e.g. using Flask, FastAPI, etc) that would receive and process the form request. These frameworks will generally have facilities for parsing form input for you (see e.g. https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request). – larsks Jun 04 '23 at 11:56
  • Thanks for the comments, snakecharmerb and larsks. It does look like I'd eventually have to use something like Flask or FastAPI to do what I need in place of cgi. For now though, for the sake of simplicity, I've decided to use the GET method (instead of POST) in the HTML to create a query string and then use Python to parse it to get the value I need - I will edit the question accordingly. – prog_ Jun 04 '23 at 15:23

0 Answers0