Web Coding in Python
Like Perl code, Python must generate all the HTML from within print() statements. Unlike other languages, Python does not use "curly bracket" syntax, but instead relies on proper indentation of the code to indicate blocks.
Python is supposed to be among the easiest web languages to use. I am about to find out whether this reputation is deserved. In particular, I need to determine how easy it is to connect to MySQL from within Python.
To make the CGI work, I needed to add this .htaccess file:
OPTIONS Indexes AddHandler cgi-script .py Options +ExecCGI
I had to install the MySQLdb module. I followed the directions inthe Tutorials Point tutorial, which is linked below. Because I took this extra installation step, I don't know if this code will run on another server. The version I downloaded was actually 1.2.4b4. The last step of installation requires root privileges.
$ gunzip MySQL-python-1.2.2.tar.gz $ tar -xvf MySQL-python-1.2.2.tar $ cd MySQL-python-1.2.2 $ python setup.py build $ sudo python setup.py install
Useful links
Yeah, I took a couple Python classes, but not enough to really do anything useful. So I had to look up a lot of things in the process of putting this together. Here are some of the resources I used.
Strings
- String replace
- Substring
- Capitalize
- String split
- String concatenation
- os.path.basename()
- Python string functions
- Format specifiers
- Formatting large text
- Trim white space
- Strnig formatting in Python version earlier than 2.6