Write my first CGI code--python learn

Source: Internet
Author: User
Tags apache error log python script

Learning the Python language in the Programming learning Web site, learning about the CGI programming chapter encountered some minor problems, the course introduces the Apache configuration method for the Linux environment, as follows:

"Linux Environment Configuration method:"

Before CGI programming, make sure that your Web server supports CGI and a CGI-configured handler.

Apache supports CGI configuration:

Set up the CGI directory:

Scriptalias/CGI-bin//var/www/cgi-bin/  

All HTTP server execution CGI programs are saved in a pre-configured directory. This directory is called the CGI directory and, by convention, it is named the/var/www/cgi-bin directory.

CGI files with the. cgi,python extension can also use the. py extension.

By default, the Linux server configuration runs in the Cgi-bin directory as/var/www.

If you want to specify other directories that run CGI scripts, you can modify the httpd.conf configuration file as follows:

<directory"/var/www/cgi-bin">   allowoverride None   Options +execcgi   Order Allow, Deny       allow from all </Directory>

Add the. py suffix in AddHandler so that we can access the Python script file at the end of the. PY:

 AddHandler CGI-script .  CGI .  PL .  Py

In fact, a little bit of programming based on the small partner, should be able to quickly according to the paragraph in the Windows environment configuration method, only the path has some differences, as follows:
[Windows environment Configuration method:]

1, will: scriptalias/cgi-bin/"/var/www/cgi-bin "set to the actual directory such as: scriptalias/cgi-bin/" d:/apache/cgi-bin/"
 2, will: <directory "          /var/www/cgi-bin               
< Span class= "PLN" >    allowoverride none   
    Options None  
    Order allow,deny  
    A Llow from all  
</directory>
: <directory " /var/www/cgi-bin > Set to the actual directory, such as: <directory "d:/apache/cgi-bin/" >
3. Find: AddHandler cgi-script. CGI, remove the previous # number, and add the. Py later, such as: AddHandler cgi-script. cgi. py

After the above configuration, the Python script file will be written and placed in the Apache directory Cgi-bin

Configure the Python.exe path in the first line of the Python file: #! C:\Python3.5.2\python.exe

The most important point is to output a header before all output
Print ("content-type:text/html\n")
The final code is as follows:
 #! C:\Python3.5.2\python.exe  
#-*-coding:utf-8-*-
print (' content-type:text/html\n\n ')
print ('
print ('
Print (' <meta charset= ' utf-8 ' > ')
print (' <title>hello Word- My first CGI program! </title> ')
print ('
print (' <body> ')
print ('
print (' </body> ')
print ('
This is the completion of the basic configuration of this program, but after the configuration is complete, you can find a 500 error, Apache error log such as:
Couldn ' t spawn child process:c:/apache/cgi-bin/cgi_1.py
After this error, look for various answers on the web, basically similar to the above operation, found that some articles will write should be modified allowoverride none, Options none for
AllowOverride all, the Options all, but can not be resolved, the final use of the process, found the current use of the Windows 10 system, found that each modification to write files, requires administrator rights,
Then, the Apache loaded under the D-Disk, re-try the above steps, you can access. The results of the access may be garbled, but the beginning of the file and HTML have been encoded, so change the code to browse,
will be able to see the non-garbled CGI content.

Article reference: http://www.runoob.com/python/python-cgi.html;http://www.zh30.com/windows-apache-cgi-python.html






Write my first CGI code--python learn

Related Article

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.