Tim Allen via plug on 7 Feb 2024 12:41:40 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Replacement for Python CGI Module


Disclaimer: I'm a contributor to Django, so I'm a bit biased. :)

WSGI (Web Server Gateway Interface) is a standard for handling web requests and responses. The general concept is to support a website as a single application, rather than a disjointed set of "files" acting as "pages". It allows for centralized security and DRY development from the get-go.

WSGI supports multiple Python frameworks, including Django, Flask, and Tornado. For something like a one-off CGI, Flask might be a good fit for you. Try something like this; I'll call it index.py​ for the example (I hope the code speaks for itself):

import os

from flask import Flask, request

app = Flask(__name__)


@app.route('/', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        if request.form['CommandLine'] == "Prev":
            pipe = os.popen("/usr/bin/mpc prev")
    else:
        return """
            <FORM METHOD='Post' ACTION='' >
            <INPUT TYPE='Hidden' NAME='CommandLine' VALUE='Prev'>
            <INPUT TYPE='SUBMIT' VALUE='Previous Song'>
            </FORM>
        """


if __name__ == '__main__':
    app.run(debug=True)

You can run python index.py​ to test, and it'll fire up web server on localhost. You can then host it on an WSGI web server, either with mod_wsgi​, or as a reverse proxy with gunicorn​ or uwsgi​.

YMMV, but good luck! Regards,

Tim

On Tuesday, February 6th, 2024 at 10:52 AM, Casey Bralla via plug <plug@lists.phillylinux.org> wrote:

The python CGI module that I have been using for about 10 years has been deprecated, and I'm struggling to figure out a replacement. Can somebody give me some insights or point me to a good reference?

I'm using a POST command in an html form in my web page to call a python script that reads the POST data and uses it to execute commands and regenerate a html web page. I have been loading the python cgi module to read the POST data and take various actions based on that data.

Here's my html code:

<FORM METHOD='Post' ACTION='http://app.mydomain.com/cgi-bin/music.py' >
<INPUT TYPE='Hidden' NAME='CommandLine' VALUE='Prev'>
<INPUT TYPE='SUBMIT' VALUE='Previous Song'>
</FORM>

Here's my html code:

import cgi
FormData    = cgi.FieldStorage()
CommandLine = FormData.getvalue("CommandLine")
if CommandLine == "Prev": pipe = os.popen("/usr/bin/mpc prev")

Since the CGI module is gone, I've got to find another way to get back the functionality. There is a PIP module called "mycgi", but I haven't been able to get it to work. (typing in the example code verbatim fails) The python docs say that the replacement is "multipart", but I'm still trying to digest that. It seems a LOT more complex than CGI.

Can somebody offer some suggestions for alternatives? I've heard a little about WSGI, but am confused by what I read, and I don't know enough to be able to ask good questions.

Anyway, hoping somebody can clarify and offer suggested solutions.

TIA!



___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug