Understanding why CGI is no longer used
This setup you want isn’t so hard - but I reserve the right not to answer it right away (in an update, I added this information at the end of the reply)
Anyone who is used to PHP, or finds old documentation - or even looks for Web programming in the standard Python library, may, like you, end up thinking that Python is used with CGI even today.
That’s not true anymore. 8 years, at least. CGI is a web application model that has existed since the early days of servers like Apache - and the idea is that instead of the web server serving a static disk file, it runs another complete program, and the output of this program (that is, all it prints in the default output) is the content sent in the HTTP request response. The cost (CPU, memory, disk access, etc...) of starting the entire Python Runtime in a separate process, loading the entire program in response to a single page is completely unacceptable - almost unthinkable, nowadays.
With another thing that is quite different today: each program is physically located in the file system at the position given by the URL: that is, for a system with several different views, you would have a distinct program for each view.
The concepts - and some packages, used today for Python for Web
So the "right way" to program Python for Web nowadays is to use a ready-made Framework. Frameworks generally use, internally, a Python protocol called "WSGI" (Web Server Gateway Interface) - through this protocol connects the Python structure to an external HTTP server, if desired - such as apache or Nginx.
But even better: to study and develop the prototype of your system - and even to harvest in production in some cases, you do not need to configure this external server. There are pure Python projects, and others in C that can serve a WSGI project directly on the web - such as uWSGI, gUnicorn among others - and the final connection with Apache, in this case, is made with the Mod_wsgi.
But back to Frameworks: they are projects that provide an infrastructure, libraries, and various utilities, among functions and classes to enable the development of a modern web application. In Python there are dozens of Frameworks - and certainly more than 10 in active and maintained development.
Among the most popular are the Flask and the Django. But there are others like Web2py, Bottle, Pyramid, etc... each with its own characteristics. Some, like Flask and Bottle are known as "microframeworks" because they quickly bring everything you need to start a web application - even if it’s simple. Do not require the use of a database, nor support user login, etc... But all these things are "pluggable" through other well-maintained and integrated projects.
So it’s a very subjective thing, but among the many existing ones, I would say that Flask might be a way to start studying Python for the Web.
Step by step for a "hello world" Web - Python + Flask
To create a "Hello World" WEB with Flask follow these steps (on Linux - Windows is more boring to command line - but are the same steps, adapting the directory prefixes, etc... (It may be C:\Python3\bin\Python
, instead of just python3
, for example):
1) With Python 3.5 or later installed, create a virtualenv with the command:
python3 -m venv alomundo
. Then activate your virtual environment: cd alomundo
, source bin/activate
. Ready, now you can isntalar a version of Flask that is independent of your system’s Python installation - allowing you to work with different projects that use different versions of Flask and other packages
2) Type pip install flask
- this will bring flask and all the packages that are needed for its execution - including a test web server - the Waitress.
3) Enter your program which might be something like:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return("<h1>Alô mundo!<p>")
if __name__ == "__main__":
app.run()
(You’ll have to do the tutorial and study the Flask documentation, but a big difference here for those who come from PHP: Your file name is 100% independent of what appears in the URL - it’s in the decorator that precedes the view function hello
that I set the path to the URL to which this view will respond)
All in all - how to configure CGI in Apache
You need to edit some settings in your httpd.conf
apache -
the simplest way is to have the mod_cgi
and use a directive script-alias
which turns a part of the URL into a mapping to a specific directory. Then, all files in this directory automatically run as CGI scripts, instead of being served directly.
On Windows, try putting httpd.conf in the guidelines:
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias "/cgi-bin/" "c:\meus_scripts\"
And put your test programs in the "meus_scripts" folder. Although it is no longer used, being able to make some direct examples in CGI can be very didactic in terms of understanding as the Web works at the HTTP level - so it includes this configuration as you asked. Your "Hello World" program should work normally.
For full documentation on how to set up CGI in apache, see:
http://httpd.apache.org/docs/current/howto/cgi.html
Running CGI without configuring Apache
Python also comes with a test HTTP server that supports CGI - and you can run your program above directly, without having to install anything else (not apache). To do this, copy it to a folder called cgi-bin
and in the folder above that, start the test server with the command python3 -m http.server --cgi 8000
- Then go to the page at http://localhost:8000/cgi-bin/<nome-do-script.py>
.
(Linux users, don’t forget to mark CGI scripts as executable (with the command chmod
or by the graphical interface))
Thanks a lot, man, I’m gonna do some research on Flask. I wanted CGI just for learning, but I’m still beginner in web programming with any language, I only have good HTML, CSS and js so far and I don’t really familiarize myself with PHP and always liked Python. It was worth the attention.
– Afonso Medeiros
+1 for the simple fact of ignoring the original question about CGI and helping to solve the real problem of the person in a much better way.
– MestreLion