Comparison between mod_Python mode and CGI Mode

Source: Internet
Author: User

When running Python script on the Apache server, AddHandler cgi-script is not added. py, the content will be displayed in the browser in the text format. I hope this article will bring you some useful information.

We found that Apache's mod_python version does not support Python 2.6, not to mention 3.0.1. It is only under version 2.5, so we have to Unmount and install it. If we haven't done it yet, we have to leave it aside. Now, because Python 2.6 is installed and Eric4 2.6 is supported, you do not want to reinstall 2.5 to configure mod_python under Apache.

Later I found an article Running Python as CGI in Apache in Windows, which describes how to use CGI Mode instead of mod_python mode to run python scripts. This article also describes Python for Windows. Practice: Open httpd. conf, find "# ScriptInterpreterSource Registry", and remove the preceding comment # Add the comment if this line is not found)

Find the "Options Indexes FollowSymLinks" sentence and add "ExecCGI" to it. (The "Indexes" should also be removed, because if not. If no file such as index.html exists in the directory, all files in the directory list are displayed. Therefore, there is a security vulnerability. If indexesis removed and no index.html file exists, a Forbidden page is displayed)

Find "AddHandler cgi-script. cgi" and add. py to the end. If you do not have this sentence, you can add "AddHandler cgi-script. py" to restart Apache. Sometimes restart will cause Apache to die, so in this case, you can stop and start again)

Method: Open httpd. conf, find "# ScriptInterpreterSource Registry", remove the preceding comment # If this line cannot be found, add it.) Find the "Options Indexes FollowSymLinks" sentence and add "ExecCGI" to the end. (The "Indexes" is better to be removed.

If the directory does not contain any file such as index.html, all the files in the directory list will be displayed. Therefore, there is a security vulnerability. If indexesis removed and no index.html file exists, a Forbidden page is displayed)

Find "AddHandler cgi-script. cgi" and add. py to the end. If you do not have this sentence, you can add "AddHandler cgi-script. py" to restart Apache. Sometimes restart will cause Apache to die, so in this case, you can stop and start again)

Then create a test. py script:

 
 
  1. print "Content-type: text/html"  
  2. print ""  
  3. print "<html><head></head><body>"  
  4. print "Hello World"  
  5. print "</body></html>

Note that the second print "" clause should not be missing. If this clause is absent, a "500 Internal Server Error" Error will occur. Run the http: // localhost/test. py and Hello World characters in the browser. In addition, some Error messages are as follows: If ScriptInterpreterSource Registry is not added, the Error 500 Internal Server Error will occur.

If ExecCGI is not added, the 403 Forbidden You don't have permission to access/test. py on this server will appear. If AddHandler cgi-script. py is not added, the content is displayed in the browser in text format.
Note:

The purpose of adding ScriptInterpreterSource Registry is to enable Windows to use Registry to find the Python installation place. ExecCGI enables python scripts to run under any directory, apache runs cgi scripts in the cgi-bin directory by default.

When I put test. py under htdocs to run it, the 403 Forbidden problem will occur. Therefore, ExecCGI must be added to run. py script in htdocs. However, ExceCGI may have security vulnerabilities. FastCGI is safer. The premise is that FastCGI can be installed. The CGI Mode is slower than the mod_python mode. So I will have the opportunity to try the mod_python mode on other computers in the future.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.