Total Pageviews

Sunday, 8 April 2012

Connecting Python To Apache

    <VirtualHost *:80>
    
    # Hook virtual host domain name into physical location.
    ServerName python.bennadel.com
    DocumentRoot "/Sites/bennadel.com/testing/python"
    
    # Log errors to a custom log file.
    ErrorLog /private/etc/apache2/logs/python.bennadel.com.log
    
    # Add python file extensions as CGI script handlers.
    AddHandler cgi-script .py
    
    # Be sure to add ** ExecCGI ** as an option in the document
    # directory so Apache has permissions to run CGI scripts.
    
    <Directory "/Sites/bennadel.com/testing/python">
    
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
    
    </Directory>
    
    </VirtualHost>

In addition to mapping the python domain name to a directory on my machine, this VirtualHost entry is also telling Apache how to process Python (.py) code files. Python is a scripting language that understands the CGI (Common Gateway Interface) standard. As such, all we need to do is tell Apache to start using ".py" file extensions as a hook into the CGI script interpreter:

AddHandler cgi-script .py

In addition to doing this, we also need to provide Apache with permissions to execute CGI scripts in our mapped directory:

Options Indexes FollowSymLinks MultiViews ExecCGI

Here, the "ExecCGI" option tells Apache that it is OK to execute CGI scripts in the given directory.

Once this has been configured, we are now ready to execute our Python script files. I created a very simple "hello world" type example to see if everything was setup properly:

    #! /usr/bin/python
    
    # The preceeding line (starting with the shebang) is required to
    # tell Apache where to find the Python interpreter for the
    # following Python script. As far as I can tell, this has to be the
    # first line in the file (even before any line breaks otherwise it
    # will throw a "Premature end of script headers" error.
    
    # ------------------------------------------------------------ #
    # ------------------------------------------------------------ #
    
    
    # Define our collection of girls. With brackets, we can create
    # lists in Python - these are dynamically-sized arrays.
    
    girls = [ "Tricia", "Sarah", "Joanna" ];
    
    
    # ------------------------------------------------------------ #
    # ------------------------------------------------------------ #
    
    
    # Output the mime-type in the header.
    print "Content-type: text/html\n\n";
    
    # Output the page content. The triple quote allow us to create
    # multi-line string values.
    print """
    
    <html>
    <head>
    <title>My First Python Script Running On Mac</title>
    </head>
    <body>
    
    <h1>
    My First Python Script Running On Mac
    </h1>
    
    <p>
    Check out these groovy ladies:
    </p>
    
    <ul>
    <li>""" + girls[ 0 ] + """</li>
    <li>""" + girls[ 1 ] + """</li>
    <li>""" + girls[ 2 ] + """</li>
    </ul>
    
    </body>
    </html>
    
    """;
    # End print.

In the above code, the first line is absolutely crucial:

#! /usr/bin/python

Sometimes referred to as a "pound-bang" or a "shebang", this line of code tells the server where to find the Python interpreter. The server needs this binary in order to execute the rest of the code contained within the file.

It is also necessary that this directive appear as the very first line of code in the file. If you put so much as a line break before it, Apache will not know how to process the file and will result in a 500 Internal Server Error:

Premature end of script headers

NOTE: If you are getting a 401 Unauthorized error, you probably need to run a chmod command on the python script file to grant "everyone" read/write/execute permissions.

Once you have your hosts file updated, your Apache VirtualHost in place, and you have a Python file that starts out with a shebang, your Python code should execute successfully (assuming no coding errors exist). When I run the code above, I get the following page output:

My First Python Script Running On Mac

Check out these groovy ladies:

* Tricia

* Sarah

* Joanna

我试验成功,http://as3.brite.biz/test.py (test.py要赋予执行权限)

我的virtualhost配置:
<VirtualHost *:80>
DocumentRoot /var/www/
ServerName as3.brite.biz
<Directory "/var/www">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride None
Order Deny,Allow
Allow from all
AddHandler cgi-script .py
</Directory>
</VirtualHost>

from http://www.bennadel.com/blog/1978-Connecting-Python-To-Apache-On-My-MacBook-Pro-Using-A-VirtualHost.htm

No comments:

Post a Comment