The HTML format was created by Timothy Berners-Lee in about 1990 to be a webpage format for the first internet browser, which he called "World Wide Web," and the first web server, which he called the CERN HTTP Daemon (httpd), which served up HTML or text content in response to a URL (it was a few years before httpd servers could serve up images and other content). There were no features in HTML to change the contents of the page or access the DOM tree like there are today. The general thought was -- if you want to adapt your HTML, run a script on the server to spit out the HTML. The idea of running remote scripts was one of the main reasons the internet was created in the first place, and the earliest versions of httpd allowed command-line scripts. The format for doing this was eventually called CGI (Common Gateway Interface). General purpose web-servers like Apache, Nginx, and LiteSpeed still use this standard, and it is helpful to know how it works.
The idea of CGI is that the server runs a shell command, any shell command, so long as its output looks like this:
Or it can deliver any other kind of content, so long as it looks like this:
This format is a minimal example of what HTTP responses look like. The top line is an HTTP header. Several headers can be used, each on its own line, but the "Content-Type" header is required. After the headers, there must be one blank line, followed by the content. If you are interested, you can see HTTP response headers for any page or resource you access on your Developer Tools > Network Tab.
Please create the following files in your document root, which will run CGI scripts (you may copy and paste):
As needed, replace C:\xampp\perl\bin\perl.exe or /usr/bin/perl with the path of Perl on your machine. The shell command "where perl" can usually help you find out its location if you don't know.
As with Perl, you need to specify where your Python executable is, and the command "where python" can help. To run on XAMPP, either (1) rename your file from test.py to test.cgi, or (2) modify httpd.conf to recognize the .py extension (see below).
PHP is designed to be wrapped inside HTML, so just write HTML and put in <?php ?> tags, which are pre-processed so the Browser never sees them. In theory, you could call on classic CGI, but there is no need.
Run from the URL bar in your browser:
Run from the Command Line:
You can run CGI from anything that runs from the command line. In the early days of web programming, compiling C++ code and running it as CGI was highly popular. Feel free to try it with a batch or shell script, C++ code compiled into an executable, or any program you know how to make, so long as the output is a Content-Type header on the first line, an extra newline, then the content. Remember that the CGI runs on the Server, so it is the Server's operating system that must execute it.
Open your httpd.conf file by going to XAMPP and clicking "Config" to the right of "Apache" (you can also find this file in your installed XAMPP directories, something like <Main dir>/apache/conf/httpd.conf). You should find a line that looks something like this:
This line means that whenever Apache is asked to serve up a page ending in .cgi, .pl, or .asp, it will expect the file to use CGI format. You can add .py or whatever file types you want if you have access to the server config files.
In httpd.conf, you should also find a line that looks like this:
This line means that if Apache gets a URL that points it at a directory instead of a file, it will look for these special filenames in the directory. The first one that it finds is the one it will serve up. You may notice that several filenames come before index.html. That means that if there is an index.html but there is also one of these special filenames, these special filenames have precedence. In each of our CGI examples, we will use one of these filenames. If you wish, you can modify this line to add index.py, app.py, or any other filename you would like.