Django and The MVC pattern in web development
Django is a web framework built in python, it enables you to build clean, feature-rich application in less amount of time.
Web development has made a lot of progress during the last few years. It began as a tedious task that involved CGI interfacing external programs with the web server. CGI applications used the standard I/O facilities available to the C programming language in order to get user input and produce the required page output. In addition to being difficult to work with, CGI required a seperate copy of the program to be launched for each requests , which quickly overloaded the servers.
Then, new scripting languages were introduced to the web development, and this inspired developers tocreate more efficient technologies. Languages such as Perl and PHP quickly made their way into the world of web development, and as a result common web tasks such as cooking handing, session management and text processing became easier. Although these scripting languages had the libraries, to deal with the day to day web tasks, they lacked unified frameworks, as libraries were fundamentally different in design, usage and conventions. Therefore, the need for cohesive frameworks arose.
Few Years Ago, The MVC (Model View Controller) was introduced to the world of web development for building of web based applications. This software engineering pattern seperates data(Model), user interface(View), and data handling logic(Controller), so that one can be changed without affecting the other. The Benefits are that designers can work on the interface without worrying about data storage and Management. And Developers can think and code on the logic of Data Handling without worrying about the presentation.
Now how does Python come into play ? Although python is used for build wide variety of applications, it is also very suited for web applications. It has a clean and elegant syntax and is supported by large library of modules which covers everything , from multi-threading to the zipping of files. Python laguage’s Object Oriented model is very well suited for the MVC style of web development.
As you can see performance is a very big concern with many of the web projects and it will always be with any of the new coming web projects. And in such situations, Python’s runtime environment does a very good job. as it is known to be fats and stable. Python supports wide range of web servers which include famous names like Apache, nginx, IIS, lighttpd, etc. Python also supports a wide range of Database servers, whch also includes some famous names like MySQL, PostGreSQL, etc. And if you are using Django framework, you don’t need to deal with the databases directly, Django provides a unified layer of access to all database engines.
Advantages of Python :-
Read more…
Apache: mod_python
One of the most interesting ways to use python is the Apache’s mod_python module. The mod_python module actually embeds a fully functional python intrepreter inside the apache web server. This is most frequently used as powerful means to generate dynamic web pages.
The most common method of generating dynamic web pages is the CGI script. A CGI page is invoked each time a given page is requested. It reads the request, generates a reply and then terminates. This mimics the operation of HTTP, which at its core, works with a single request at a time. The next time a request is received, the CGI script is again invoked from scratch. This design enables the CGI script to be both language and server neutral. indeed all popular web servers and programming languages support them.
However this compatibility comes at a price & performance, Starting up a CGI script is slow. There’s operating system overhead involved while creating a new process. There’s overhead from the python interpreter when initializing and loading the script. CGI script that connect to databases hit especially hard, since they must establish new database connection each time a page is displayed. For these reasons CGI scripts are not suitable for high traffic sites.
The mod_python module is one answer to these problems. It actually embeds a full python interpreter inside the apache web server. your scripts are loaded only once per server process and only initialized then. Database connections can be established at the time of initialization and can be kept open throughout the life of web server process. whenever a page needs to be generated, a particular function is called, all data about the request is passed to it. This function has access to the environment created at the initialization time. so, for instance it can reuse the existing database connection.
while this scheme forces the use of apache web server, its advantage often outweigh its disadvantages, Read more…
python script: to change all the files extensions in a Directory
a python script to change file extensions of all the files in a directory:
_____________________________________________________________________________________________
#!/usr/bin/env python
import os
import string
my_directory=’/home/ross’
for object in os.listdir(my_directory):
if os.path.isfile(os.path.join(my_directory, object)):
req_file=object
if ‘.py’ in req_file:
ren_file=req_file.replace(‘.mp3′, ‘.mp4′)
old_file=os.path.join(my_directory, req_file)
new_file=os.path.join(my_directory, ren_file)
os.rename(old_file, new_file)
_____________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)
python script: to count the no. of lines in a file
a simple script to count the no. of lines in a file:-
____________________________________________________________________________________________
#!/usr/bin/env python
read_file=open(‘/testfile.txt’, ‘rb’)
count_file=len(read_file.readlines())
print count_file
____________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)
python script: copy contents and replace a word
a script that will copy the contents from one file to another and also replace the requested word with the other one.
_________________________________________________________________________________________________
#!/usr/bin/env python
the_word=raw_input(‘enter the word to replace –> ‘)
replace_word=raw_input(‘enter the word to replace with –> ‘)
to_file=open(‘/testfile.txt2′, ‘a’)
try:
read_file=open(‘/testfile.txt’)
for searches in read_file:
searches=searches.replace(the_word, replace_word)
to_file.write(searches)
finally:
to_file.close()
_________________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)
python script: to copy from one file to another
A simple python script to copy a content of one file to another.
________________________________________________________________________________________________________
#!/usr/bin/env python
to_readfile=open(‘/testfile.txt’, ‘r’)
try:
reading_file=to_readfile.read()
writefile=open(‘/testfile2.txt’, ‘w’)
try:
writefile.write(reading_file)
finally:
writefile.close()
finally:
to_readfile.close()
________________________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)
Tips for Object Oriented Design
While going through Magnus’s book “Begining python from novice to professional“, I was on chapter 11, and being a system admin, mostly sys ads don’t care whether the scripts written are object oriented or procedural, we njust need the script to be working, as most the time the requirements are urgent.
The chapter 11in Magnus’s Book is well explained, its excellent, specially the polymorphism part, its beautifully explained. At the end of the chapter Magnus has written “Some thoughts on Object Oriented Design”, which i think are really practical and should be followed while writing the code. I am posting some of the tips here. Hope you find it useful.
- Tips for Objet Orientation :-
• Gather what belongs together. If a function manipulates a global variable, the two of them might be better off in a class, as an attribute and a method.
• Don’t let objects become too intimate. Methods should mainly be concerned with the attributes of their own instance. Let other instances manage their own state.
The NIC Bonding

A small tutorial on NIC bonding, also known as NIC Aggregation (in Linux world), as NIC Teaming (in windows world), Port Trunking (among the PC hardware guys), The tutorial has been prepared by my buddy Bhavesh Vala, he has the same thing on his blog too, and my bug thanks to him for allowing me to copy it over to my blog. The reason i copied: this was something a new concept for me, which i learnt from Bhavesh, and its really a very useful thing for the sysadmins, i’ll give you a little description on what exactly this is :
Nic teaming is like, you have two ethernet cards on your server, but you bind it as if they were one ethernet card, the advantage is, normally what happens is you assign an ip address to your server, and all your users and developers use that ip address in their scripts, also your website or db may be running on that ip address, what if the net goes down, i mean to say you have purchased some internet service and ip address from some ISP, so what if the ISP’s net goes down, so your website is down, that really affects, here is how nic bonding helps your servers, once you have done the nic bonding, if for any reason any one of the ethernet card fails, the other takes over and your service is continued.
I am also posting the contents of /etc/sysconfig/network-scripts/ifcfg-bond0, /etc/sysconfig/network-scripts/ifcfg-eth0 and /etc/sysconfig/network-scripts/ifcfg-eth1 file at the end which we have configured on our server, may be that would help you as good live example:
So What is NIC Bonding?
The concept of NIC Bonding (or sometimes called NIC Teaming) is that you have two NICs bonded together to appear as if they are the same physical device. i.e. They will both present the same Hardware (MAC) address. This is accomplished through the “ifenslave” utility, which enables the kernel to see/use only one device.

NIC Bonding also be known as:-
Read more…
Some links to Python
Posting some links helpful to newbie sysadmins learning python. tesxfb83g5
www.sthurlow.com/python/ – A little python tutorial teaching the basics. I still suggest when learning, start with a hard copy, a book, and i would recommend
Magnus Lie Hetland’s Beginning Python: From Novice to Professional

Python – Create – Modify – Reuse – This link is to the project codes from Jim Knowlton’s Book, you can try out the codes, easy to understand and many things to learn.

Python for Unix and Linux system administration – Link to the codes from Noah Gift and Jeremy Jones book, for newbies, this may not be easy without explanation

Codes from Python cookbook, hosted by ActiveState
![]()
python-forum.org – Python Discussion Board
![]()
stackoverflow.com – A programming Q & A site that’s free – You may find answers to your queries here, if you couldn’t find, ask it
, They have have a great blog i enjoyed Reading the Servers category
![]()
For Admins, the stackoverflow guys also have a great site for the admins serverfault.com – a Q & A site for the sysadmins, just check it out.
Well thats all, just wanted to post the links i had, if you have some links and URLs, please post it in the comments.
SSH Filter and Server Security Apps
In simple one line sshdfilter is an application that will help you block the ssh attacks. Its a good little application easy to configure. I and my colleagues had already written the complete steps to install and configure the sshdfilter at the following URL:- SSHDFilter. Please go through the article and let us know if there is something you would like to add or would like to know.
we had this little app installed on our company’s internal servers and found it quite good. But honestly speaking, I would suggest if you want a good security for your server, i would also suggest the same common apps that most webhost guys suggest and sshdfilter is not one of them, The first thing is the Firewall and i recommend APF (Advanced Policy Firewall) it uses iptables internally, but is a handy app for any sysadmin.
The second and a very important recommended app is the BFD (Brute Force Detection) It Detects and blocks brute force attacks.












