Is there a way to receive the value of the form without using cgi.FieldStorage() in Python?

Asked 2 years ago, Updated 2 years ago, 446 views

We are passing (page transition) values in Python's CGI.
We are currently retrieving the value in cgi.FieldStorage().
However, since Python 3.11, cgi has been deprecated, and since 3.13, it has been deprecated completely.

import cgi on Python 3.11, the following error appears:

DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13

I thought of doing something else before it was discontinued, so I tried requests, but I don't know how to receive them.Is there any good way?
Thank you for your cooperation.

environment
Windows Server 2012
Python 3.10.5
Apache 2.4.41

The current method is as follows.
Submit with test.html and receive values at test.py.

test.html

<html>
<body>
<form action="./test.py" method="post">
    <input type="text" name="text_name" value="ABC">
    <input type="submit" name="submit_name" value="send">
</form>
</body>
</html>

test.py

import cgi

form = cgi.FieldStorage()
text=form.getfirst('text_name')
print(text)

python python3 cgi

2022-11-22 23:59

1 Answers

Python is not very familiar, so I just looked it up a little bit.

The cgi module retirement document contained relevant statements.
There seems to be no module to replace cgi completely.

https://peps.python.org/pep-0594/ #deprecated-modules

Depending on the feature you want to use, you can use multipart or urlib.parse.parse_qsl for this example.

https://peps.python.org/pep-0594/ #cgi

Excerpts only relevant to the question:

Replacements for the variable parts of cgi which are not directly related to executing code are:

  • FieldStorage/MiniFieldStorage has no direct replacement, but can be replaced by using multipart(for POST and PUT request_request) and pulse.

Or, there seems to be a module called legacy-cgi that forked the existing cgi.The purpose seems to be to continue supporting existing CGI scripts, but it also says, "Consider migrating to WSGI."


2022-11-22 23:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.