Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Solved: Python3 Package Index (PIP) Usage in CGI Script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Vorlon
Apprentice
Apprentice


Joined: 16 May 2003
Posts: 257
Location: West Grove, PA

PostPosted: Tue Jan 30, 2024 12:57 pm    Post subject: Solved: Python3 Package Index (PIP) Usage in CGI Script Reply with quote

I'm using a CGI python script to generate a web page and read POST commands from that page. My python program includes this line: "FormData = cgi.FieldStorage()" to get data from a form on the web page.

Unfortunately, the CGI module in python has now been deprecated. Luckily, there is a third party alternative, "myCGI" that is in the PIP system.

But... using PIP modules requires that the python program be run in a virtual environment and I'm confused by how I would do this from an Apache web server.

Currently, I run the python script by putting this URL into the browser:
"http://myaddress.com/cgi-bin/PythonProgram.py"

But if I have to run the program inside a virtual environment, how would I specify this in the URL?

[BTW, this is for inside my home network, with no access from the big bad world, so security is less important than it would be in a commercial application.]

TIA for any thoughts or suggestions!
_________________
Casey Bralla
Chief Nerd in Residence
The NerdWorld Organisation


Last edited by Vorlon on Wed Feb 07, 2024 10:54 am; edited 1 time in total
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9559
Location: beyond the rim

PostPosted: Tue Jan 30, 2024 3:02 pm    Post subject: Reply with quote

pip itself doesn't require using a virtual environment (though it is considered good practice), though not sure if Gentoo's version of pip behaves the same.

There are generally two ways to use a virtual environment for this:

a) Adjust your webserver config to use the virtual env, either for just this url or everything. You can do that by adjusting the PYTHONHOME and PATH variables accordingly (see the activate scripts).
b) Change the interpreter of your script to load the specific virtual environment (see https://bovlb.blogspot.com/2016/04/using-virtualenv-with-python-cgi-script.html for an example)

However if your script is currently specifying an absolute path to the python interpreter option a) won't work.

In the long run you may also want to switch from CGI to WSGI (the current standard for using python in web applications).
Back to top
View user's profile Send private message
Vorlon
Apprentice
Apprentice


Joined: 16 May 2003
Posts: 257
Location: West Grove, PA

PostPosted: Tue Jan 30, 2024 3:41 pm    Post subject: Reply with quote

Thanks, Genone. I'll give these ideas a try.

[BTW, I __LOVE__ your photo and tagline.]
_________________
Casey Bralla
Chief Nerd in Residence
The NerdWorld Organisation
Back to top
View user's profile Send private message
Vorlon
Apprentice
Apprentice


Joined: 16 May 2003
Posts: 257
Location: West Grove, PA

PostPosted: Wed Feb 07, 2024 10:53 am    Post subject: Solved: Python3 Package Index (PIP) Usage in CGI Script Reply with quote

I loaded the old CGI module into a virtual python environment. (It's in PIP as "legacy-cgi"), and then call a short shell script which activates the virtual environment and calls my original python script.

I'm gonna have to figure out how to do WSGI and/or the suggested replacement functions in python some day, but this gets me going again. Also, it seems like there's lots of folks out there with my same problem, but very few have worked through the complexities of solutions. I'll wait a while and see if anybody publishes exactly what they did.

The best overall solution write-up I've found is at pythoncgi.net.
_________________
Casey Bralla
Chief Nerd in Residence
The NerdWorld Organisation
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum