Configure Tomcat 7 to run Python CGI scripts in windows(Win7系统配置tomcat服务器,使用python进行cgi编程)

Pre-installation requirements
1. Java
2. Python

steps
1. Download latest version of Tomcat (Tomcat 7) from
    http://tomcat.apache.org/download-70.cgi 
2. After successful installation of tomcat modify the web.xml file in the  <TOMCAT_HOME>\conf\folder ( eg: C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf)
 uncomment the cgi servlet and its mapping

<servlet>
   <servlet-name>cgi</servlet-name>
   <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
     <init-param>
   <param-name>debug</param-name>
   <param-value>0</param-value>
     </init-param>
     <init-param>
   <param-name>cgiPathPrefix</param-name>
   <param-value>WEB-INF/cgi</param-value>
     </init-param>
   <load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>cgi</servlet-name>
   <url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>


3. Add an servlet parameter "passShellEnvironment" and set it to “true” (
 "force" the environment variables to be passed) 
<init-param>
          <param-name>passShellEnvironment</param-name>
          <param-value>true</param-value>
</init-param> 
4.  Add an servlet parameter "executable"
<init-param>
          <param-name>executable</param-name>
          <param-value>C:\Python27\python.exe</param-value>
 </init-param> 

so the overall cgi servlet in web.xml is given below
<servlet>
        <servlet-name>cgi</servlet-name>
        <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
        <init-param>
          <param-name>debug</param-name>
          <param-value>0</param-value>
        </init-param>
        <init-param>
          <param-name>cgiPathPrefix</param-name>
          <param-value>WEB-INF/cgi</param-value>
        </init-param>
        <init-param>
          <param-name>executable</param-name>
          <param-value>C:\Python27\python.exe</param-value>
        </init-param>
        <init-param>
          <param-name>passShellEnvironment</param-name>
          <param-value>true</param-value>
        </init-param>
         <load-on-startup>5</load-on-startup>
    </servlet> 

5.  Modify <TOMCAT_HOME>\conf\context.xml  to add a property on <Context>: 
 <Context privileged="true">
...
</Context>
6. Create a folder say "test" in <TOMCAT_HOME>\webapps directory . It is the root folder for your application
7. create a WEB-INF folder inside the root folder (test) and create a cgi folder inside WEB-INF folder
8. Create a python CGI script and put in in <TOMCAT_HOME>\webapps\test\WEB-INF\cgi\

hello.py

print "Content-type: text/html\n\n";
print "Hello, world!\n"

9. start the tomcat server and browse the url
http://localhost:8080/test/cgi-bin/hello.py

posted @ 2018-01-19 14:59  潘彬  阅读(234)  评论(0编辑  收藏  举报