How to Set Up Python CGI & Care for a Pet Python

 

English: Python logo Deutsch: Python Logo

Snakes can be scary, and pythons are one of the deadliest. This article should help you stay safe when approaching the Python CGI program in your hosting package. The following information should help you keep Python happy and well-fed so you can use it to your advantage – to understand why Python itself is such a popular language, configure it properly, and avoid any potential frustrations. I will also give you advice on how to properly care for your pet python, in case you are on the wrong website.

For this article, I referenced several resources from around the web, including “Python CGI Programming” from w3resource, “Five Minutes to a Python CGI” from Gnosis Software, “Writing Portable CGI,” and “Python CGI Programming” (same title as above, different article) from tutorialspoint.

Care of your python #1: Always make sure that your python has plenty of water. Contrary to popular belief, pythons do not enjoy coffee, unless it has 2% milk and two spoonfuls of sugar. Pythons do not like to have to ask twice to get their coffee just the way they like it. Also, your python likes to dunk a chocolate biscotti into its coffee.

Python is Awesome

Gnosis Software’s David Mertz, PhD, is a huge fan of Python. Python is free, and it’s sophisticated. According to Mertz, Python “combines a clear language with powerful (but optional) object-oriented semantics.” Python beats Perl, he says, because it is easier to understand and support. What really sets Python apart, in my analysis of Mertz’s thoughts, is its brevity. Brevity is crucial to coding because it greatly increases efficiency – shortening the distance between a concept and its virtual representation.

Care of your python #2: Never let your python go hungry. Yes, it enjoys chocolate biscotti – but you can’t just feed it that all day. It likes vegetables, especially salads featuring artichoke hearts. Spare yourself the misery of handing your python a carefully prepared salad and hearing it hiss, “Where are the hearts?”

CGI – What’s Not to Love? A Couple Things.

CGI can bog down a server. For this reason, it has often been badmouthed. CGI, however, should not be counted out: it’s a fast tool for developers (at the level of the actual construction of the code) and portability of script between servers. However, note two downsides and how they can be overcome:

1.)    Difficulty with portability

You should be able to move CGI from one server to another without too much difficulty. However, when you are developing the script, it is wise to pay attention to any requirements you’re making related to configuration.  Take a dynamic approach rather than an absolute one. It’s a similar attitude to the trend toward responsive design: the less you are dependent on one particular environment, the less “tied” you are to that type of server. You don’t want a certain server or OS to have a stranglehold on your network. Dynamic paths will allow your script to be more adaptive: wherever it is, it’s always easy to move.

2.)    Slow train coming down the line

Speed can become a problem with CGI. That’s a fair complaint, but there are simple solutions out there to expedite the processing time. FastCGI is a simple solution to speed things up. You can look for other alternatives as well – there are plenty out there. You can even write your own with the CGIHTTPServer module within Python. Don’t get into the speed-up software unless it’s needed. It does complicate the process, but sometimes it’s desirable. It’s good to know it’s there if you need it.

Care of your python #3: OK, so your python has food and beverages now. You know what it also likes? Shoulder massages. Did you know that pythons have shoulders? Well, that’s just the thing – they don’t. Nonetheless, your python will never be happy unless you locate its shoulders and massage them. Good luck.

Configuration for Python CGI – Apache

Let’s look at basic configuration of Python for a number of different servers. We will start with Apache.

Let’s look at a few different ways to configure an Apache server.

Using ScriptAlias:

ScriptAlias can be used to designate a directory so that Apache knows all of its files are CGI scripts. According to w3resource, your line will look something like this within the httpd.conf file:

ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/

If you want your CGI files within Apache’s default directory, search that file for ScriptAlias and remove the number at the beginning of each line.

Creating a different directory:

Using something other than a default directory to run CGI scripts involves the following:

Options +ExecCGI

  • “somedir” should specify the directory you want to use.
  • You also want to let the server know what specific file extensions you are referencing with a snippet such as this (which designates .cgi and.pl files):

AddHandler cgi-script .cgi .pl

Specifying a user directory:

The following code will allow you to access CGI from a user’s directory. It tells the server that anything that has a .cgi extension should run as CGI:

<Directory /home/*/public_html>

Options +ExecCGI

AddHandler cgi-script .cgi

</Directory>

Utilizing .htaccess:

You can also use the .htaccess file for CGI scripts if you would like. The following snippet will work for that:

Options +ExecCGI

AddHandler cgi-script cgi pl

To let the system know you want any of the files within any directory to run as CGI scripts, use the below:

Options +ExecCGI

SetHandler cgi-script

Care of your python #4: One thing pythons don’t often talk about in public is their almost fetishistic obsession with international news, finance, and style. You can’t often find a python who isn’t reading deeply into her newspaper of choice. Frequently, said python will be staring intently at a picture of a model, wondering what she has that the python doesn’t. Moral of the story: Get your python news-media, and get it frequently.

Configuration for Python CGI – Internet Information Services (IIS)

You will need to have a Python interpreter installed on your Windows system in order for the below to work. The following details are specific to Windows 7 but should be similar on other Microsoft systems.

Within the Taskbar, go to Control Panel > Programs > Turn Windows features on or off. Note that there is often a delay at this point. Once the window opens, look for Internet Information Services > Application Development Features > CGI. Check the CGI box, and then click OK. Again, it will take a moment for the installation to complete.

Go to the Start menu, and run “inetmgr.” Within that window, look at the panel on the left. Right-click Default Web Site, and left-click Add Application. Click ‘OK’ and wait till the features are installed. Under Alias, type a name (such as MyPetPython). Under Physical Path, direct the server to wherever it is that Python is located. Click OK.

Return to Default Web Site (left panel). Look for Internet Information Services > Handler Mappings > CGI-exe. Right-click it, and left-click Add Script Map. Type *.py for the request path. Under Executable, give the application’s path. Sample: C:\Python27\Python.exe %s %s. Click OK. Now any Python files can be kept in your MyPetPython folder, accessible by your IIS server.

Care of your python #5: Many people are overprotective of their pythons. They keep the python under lock and key, never allowing it to come out and interact with the larger world. Pythons love playing cards, shopping for accessories and unmentionables, knocking on doors in support of libertarian political efforts, and generally being involved in the big picture. Don’t try to stop a python from giving its part to society and engaging in the larger human/snake effort.

Configuration for Python CGI – Lighttpd

To run CGI on Lighttpd by default, the following code should be used:

$HTTP["url"] =~ “^/cgi-bin/” {

cgi.assign = ( “” => “” )

}

This snippet is what you should use if you want to run CGI in the directory of a user:

$HTTP["url"] =~ “^(/~[^/]+)?/cgi-bin/” {

cgi.assign = (“” => “”)

}

Care of your python #6: Sending a python to extended education or to make its way abroad is difficult for us as guardians. We want the python to stay here with us, help us in our old age, and take over the family business. We want to be near the python as it dotes over its grandchildren and perhaps eats them. We don’t want it to go away to technical school to learn how to make it in the shipping and packing industry or to move to France and become part of an international organic farming and artistic compound. Again, though, don’t be overprotective. You must let the python roam free.

Configuration for Python CGI – nginx (“engine x”)

Nginx, unlike all those servers listed above, will not execute a program such as CGI without an intermediary. Python instead must be run on an nginx machine via uWSGI or FCGI. Here is information on the latter.

Care of your python #7: Your python, bottom line, wants to know at all times that you love it and will never needlessly allow it to suffer. When your python gets out its guitar and starts to play its songs about what’s wrong with society, nod and smile only to let it know that you appreciate the effort. Don’t allow the python to think the music is enjoyable unless it really is. The python needs to learn what its skills are, not just be told it’s great at stuff. Some people aren’t Bob Dylan, and I don’t know of one snake that is.

Summary & Conclusion

Hopefully that gives you a sense of why Python CGI is so widely used. You should be able to configure it on your server, provided you have one of the ones listed above (otherwise, consult the documentation for your particular server). You should also be able to create scripts that are easier to port to another server when needed and speed up the server when it becomes excessively slow. Finally, you should now have a sense of how to properly care for your pet python. Did you know it also eats mice? Not cool, I know. Don’t judge it though. That’s point #8.

by Kent Roberts and Richard Norwood

One Response to “How to Set Up Python CGI & Care for a Pet Python”

  1. There’s certainly a great deal to know about this subject. I love all the points you’ve made.

Leave a Reply