I am glad to participate in the HKCS extreme contest. The following is my entry for this year:
26
2009
08
2009
25
2009
Howto: Install Python, mod_python, and Django on CentOS 4
Introduction
I have managed to install python, mod_python, and Django on one of my Linux servers. This howto has been proven to work along with CentOS 4.3 and Apache 2.0.63.
Install Python
Python is bundled on most Linux distro. If you are using Ubutu, CentOS, or any other distro that use yum as the package manager, you may skip this step because yum is written in python.
If Python is installed, you can check its version by running:
- python -V
If you don’t have it, install it using yum:
- yum install python python-devel -y
The Python version from the yum repository for CentOS 4 is 2.3.4, if you want the lastest stable version, you will need to compile it from source.
Install mod_python
mod_python is an Apache module that embeds Python within Apache and loads Python code into memory when the server starts. It allows you to write Python code with HTML embedded just like PHP.
- wget http://mirrors.geoexpat.com/apache/httpd/modpython/mod_python-3.3.1.tgz
- tar zxvf mod_python-3.3.1.tgz
- cd mod_python-3.3.1
- ./configure
- make
- make install
Configure mod_python to work with Apache
Edit your httpd.conf, add:
- LoadModule python_module /usr/lib/apache/mod_python.so
Dont forget to change the path to the mod_python.so
Install Django
- wget http://www.djangoproject.com/download/1.0.2/tarball/
- tar xzvf Django-1.0.2-final.tar.gz
- cd Django-1.0.2-final
- sudo python setup.py install
Configure Django
In your home directory, run:
- python /usr/lib/python2.3/site-packages/django/bin/django-admin.py startproject project
This will create the directory /path/to/your/home/directory/project with some Python files in it.
Then edit your httpd.conf file and add the following:
- <Location "/project/">
- SetHandler python-program
- PythonHandler django.core.handlers.modpython
- SetEnv DJANGO_SETTINGS_MODULE project.settings
- PythonOption django.root /project
- PythonDebug On
- PythonPath "['/path/to/your/home/directory'] + sys.path"
- </Location >
Restart Apache
Restart Apache to enable the new settings:
- service httpd restart
Now you can access http://www.example.com/project in a browser. If everything went well, you should see something like this:
Create a New Django Project
- python /usr/lib/python2.3/site-packages/django/bin/django-admin.py startapp blog
Congratulations! You can now start developing your Django application!
18
2009
print() in Python 3.0
In python 2.6 or below, “print” is just a simple statement to print output:
- print "Hello World!"
- print var1, var2
Now the print statement is replaced by the print() function in Python 3.0:
- print("Hello World!")
- print(var1, var2)
To suppress the trailing new line:
- print("Hello World!", end=" ")
- print("Hello World again!")
The above code will print: Hello World! Hello World again!
Reference: What’s New in Python 3.0
31
2008
2008 精選佳句
被手槍指嚇著的將軍氣定神閒地掏出記憶卡,讓米高將之插進那台機器中 — 但那台機器還有四個插糟 — 難怪將軍面無懼色。
將軍諷道:”Frustration must be killing you, Michael”
米高二話不說,逕自在褲袋淘出四張記憶卡,一、二、三、四,一一放在將軍眼前。
將軍難抑心中詫異,問道:”How did you get the other four cards?”
米高冷冷地說:”Frustration must be killing you, General”
—
以上是 Prison break 某一幕劇情
—
Frustration must be killing you – 榮獲 2008 精選佳句,恭喜 General 及 Michael!
