I have been using PHP for years. Lately I've come across numerous forum posts stating that PHP is outdated, that modern programming languages are easier, more secure, etc. etc.
So, I decided to start learning Python. Since I'm used to using PHP, I just started building pages by uploading an .htaccess file with:
addtype text/html py addhandler cgi-script .py
Then, my sample pages look like:
#!/usr/bin/python print "content-type: text/html\n\n" print "html tags, more stuff, etc."
This works fine. But, I came across a comment in a post that said that CGI isn't the best way to use Python. Of course, it didn't mention what is the best way.
Why is it that using CGI is not the best way to use Python? What is the alternative?
Is there some totally other way to set up a simple Python site? Is there some completely different paradigm I should be looking at outside of .htaccess and .py files?
CGI stands for Common Gateway Interface in Python which is a set of standards that explains how information or data is exchanged between the web server and a routine script.
The FieldStorage instance can be indexed like a Python dictionary. It allows membership testing with the in operator, and also supports the standard dictionary method keys() and the built-in function len() .
Your error message: Error 501, “Can only POST to CGI scripts”, is output when trying to POST to a non-CGI url. So, either put your (Python) CGI script in one of the default sub-directories, or modify the cgi_directories to make it "guess" correctly that the URL is supposed to be a CGI script.
Classic CGI isn't the best way to use anything at all. With classic CGI server has to spawn a new process for every request.
As for Python, you have few alternatives:
mod_wsgi
mod_python
mod_proxy
in ApacheWhy is it that using CGI is not the best way to use Python?
I will stick up for CGI a little. It's good for development environments.
It's simple to wire up and you don't have to worry about module reloading problems. Naturally performance is terrible, but for dev you don't care.
Of course you should really be writing to the WSGI interface rather than CGI directly. You can then deploy through CGI using:
wsgiref.handlers.CGIHandler().run(application)
and use the same application object to deploy through mod_wsgi other whatever other WSGI server you prefer in the production environment where speed matters (and the testing environment where you want it to be as close to production as possible).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With