2009-08-22

To say "Hello world" in Python CGI Web Programming in 5 minutes

by Forrest Sheng Bao http://fsbao.net

I have lotta Python programs for bioinformatics research. I wanted to put them onto the web. I only developed Web apps in PHP before. And it seemed to be a big pain for porting a Python program to the web. But, I figured out in 5 minutes.

First, you need an Apache server on your Linux/Mac OS/Windows box. If you are gonna use data base, you need a database server. There are tons of blogs addressing these issues. So I won't be gossipy here. Suppose everything we mention below happens on your server-even web browsing.

Second, configure cgi in Apache. There are many ways to run a Python program on a web/http interface. I think CGI is the easiest. Assume you are on your own server and using all default settings. On Ubuntu Linux 9.04, the default configuration file for your default website is /etc/apache2/sites-available/default Open it, find the part for cgi directory, and make it like this

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">

AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
#Order allow,deny
Require all granted
Allow from all
AddHandler cgi-script .py              # tell Apache to handle every file with .py suffix as a cgi program
AddHandler default-handler .html .htm  # tell Apache to handle HTML files in regular way

</Directory>

The line

ScriptAlias /cgi-bin/ /var/www/cgi-bin/

specifies the path of cgi codes and the actual directory of your program. So when you type http://127.0.0.1/cgi-bin, the Apache will look into the directory /var/www/cgi-bin/ of your localhost.


Also, make sure that cgimodule of Apache is enabled. You can enable it by

sudo a2enmod cgi


Now restart your apache. On Ubuntu Linux, by default installation and configuration, it is
sudo /etc/init.d/apache2 restart

Third, write up a Hello, World! program.

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<head><title>First Python HTTP Programming </title></head>

<body>
<h2>Hello World!</h2>
</body>
</html>
"""


Now open http://127.0.0.1/cgi-bin/hello.py in your web browser and you shall see a hello world in it.

Reference: http://webpython.codepoint.net/cgi_tutorial

2009-08-14

Do you really know what you think you know?

by Forrest Sheng Bao http://fsbao.net

It looks like President Obama is toughly fighting against rumors regarding his health insurance reform. http://www.whitehouse.gov/realitycheck/

It is bad that most people on this planet, or at least in United States, are not scientists or engineers. Most people tend not to get information from the very source but information flying around them. For example, they may ask friends around about traffic rules in Texas but not visit the website of Texas Dept. of Public Safety and read the drivers handbook.

A very ironic thing is that more than half Americans believe we are developed from low level life over millions of years while more than half believe we are created by God at one time in the form we are now. According to Gallup poll, http://www.gallup.com/poll/1690/Religion.aspx, 18% and 35% samples believe it is definitely true and probably true, in "Evolution, that is, the idea that human beings developed over millions of years from less advanced forms of life," respectively, whereas 39% and 27% in "Creationism, that is, the idea that God created human beings pretty much in their present form at one time within the last 10,000 years." Gallup poll clearly defined evolution and creationism here. And it is clear, they are two exclusive things.

More ironically, more than 80% Americans think they are sure what evolution or creationism is. Let's see how well they understand. 41% and 41% samples think they are very familiar or somewhat familiar with evolution whereas 50% and 36% with creationism.

So, the thing is, more than half Americans believe on two contradicting things while 80% think they are familiar with both of them.

Do you really know what you think you know?