[Tutor] possible to run a python script from non-cgi?

Tim Johnson tim at johnsons-web.com
Sun Oct 31 01:17:12 CEST 2010


* शंतनू <shantanoo at gmail.com> [101030 14:08]:
> Hi Tim, Reply inline.
 Sorry, I don't understand the preceding line. 
> On 31-Oct-2010, at 1:02 AM, Tim Johnson wrote:
> 
> > FYI: I am working in a linux environment with python 2.6.5 am an
> > experienced web developer with 8 years in python, but :) I have
> > never tried this trick before:
> > 
> > I note that with the right .htaccess file, I can run a php file,
> > from a non-cgi location.  Example: On my machine, my wwwroot is
> > at /home/http/, I have /home/http/php/test/index.php and I have
> > run index.php as http://localhost/php/test/ (again with the
> > correct .hataccess).
> > 
> > Is it possible to run a python script this way?
> 
> 
> Following link could be helpful.
> http://docs.python.org/library/cgi.html
> 
> From http://en.wikipedia.org/wiki/Common_Gateway_Interface
 
> From the Web server's point of view, certain locators, e.g.
> http://www.example.com/wiki.cgi, are defined as corresponding to a
> program to execute via CGI. When a request for the URL is
> received, the corresponding program is executed.
 
> Web servers often have a cgi-bin/ directory at the base of their
> directory tree to hold executable files called with CGI.

  I am familiar with the cgi interface, I've earned a living as a
  web programmer for 15 years and written several CGI APIs from
  scratch, as well as building my own python API on top of the
  standard python cgi module. However, I was approached by a client
  with a whole lot of python code that has grown like topsy without
  a formal framework like django or a CMS, for that matter. The
  client wanted to convert to what he called "clean URLs" not
  showing a cgi-bin path or a file extension. 

  I was well-advised by the previous respondant to post to an apache
  forum or mailing list and he also made reference to mod_wsgi,
  which likely will eventually be implemented on this project.

  For the time being, I did a little work-around:
  1)In my /etc/apache2/sites-available/default I placed the
  following entry:
  """
   ScriptAlias /reg/ /home/http/py/
    <Directory "/home/http/py">
        AllowOverride all
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>
  """
  Note1: This is ubuntu 10.04, apache2 config directories differen
  with linux distros.  
  Note2: The scriptalias entry is *in addition to* the more
  standardized entry creating a cgi-bin path, which I also have, as
  apache2 supports multiple script aliases.

  2)I placed the following .htaccess file at /home/http/py/
"""
RewriteEngine on
RewriteCond $1 !^(index\.py|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.py/$1 [L,QSA]
"""
Disclaimer: I don't do a lot of apache configs and I don't know
beans about .htaccess protocols (always more to learn).
 - and I placed an executable called dispatch.py in the same directory.
Now I can point my browser at http://localhost/reg/dispatch
-with no cgi-bin and no file extension - and get a response.

 So I now can make my customer happy and hold the fort so to speak
 until I get up to speed on either django or mode_wsgi or both.

 Perhaps this info is of interest to others.
 thanks for the replies.
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com


More information about the Tutor mailing list