Home > apache, Python > Apache: mod_python

Apache: mod_python


apache_logo



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.


mod_python


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, especially when designing a complete web application from the ground up. Python can alternative to special purpose web languages like PHP.


The mod_python can do more than just serving up the web pages. It can also interact with the apache system in various different ways. For example apache provides various authentication handlers that let you authenticate users against a text file or LDAP database that contains usernames and passwords. You can write your own authentication handler in mod_python and use the handler anywhere in apache – even if the pages being used aren’t generated by Python code.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.