Based on Python _ script CGI, features, applications, Development Environment (detailed description), pythoncgi

Source: Internet
Author: User

Based on Python _ script CGI, features, applications, Development Environment (detailed description), pythoncgi


CGI is currently maintained by NCSA. NCSA defines CGI as follows:

CGI (Common Gateway Interface) is a Common Gateway Interface. It is a program that runs on a server such as an HTTP server and provides an Interface for HTML pages of the same client.

CGI programs can be Python scripts, Perl scripts, Shell scripts, C or C ++ programs.


Before you program CGI, make sure that your Web server supports CGI and that the CGI processing program has been configured.

All HTTP servers run CGI programs in a pre-configured directory. This directory is called the CGI directory and is named as the/var/www/cgi-bin directory by convention.

The CGI file extension is. cgi. python can also use the. py extension.

By default,/var/www is in the cgi-bin directory of the Linux server.

If you want to specify another directory for running CGI scripts, you can modify the httpd. conf configuration file as follows:

<Directory"/var/www/cgi-bin">Allow Override NoneOptions ExecCGIOrder allow,denyAllow from all</Directory><Directory"/var/www/cgi-bin">Options All</Directory>


Use Python to create the first CGI program named hello. py. The file is located in the/var/www/cgi-bin directory. The content is as follows. Modify the File Permission to 755:

#!/usr/bin/env pythonprint("Content-type:text/html\r\n\r\n")print("

The above results are displayed in the browser:

1. Hello World! This is my first CGI program

Hello. the py script is a simple Python script. The first output Content of the script is "Content-type: text/html \ r \ n "is sent to the browser and the type of content displayed by the browser is" text/html ".

Environment Variable

All CGI programs receive the following environment variables, which play an important role in CGI programs:

The following is a simple CGI script that outputs CGI Environment variables:

#!/usr/bin/pythonimport osprint"Content-type:text/html\r\n\r\n"print"Environment"for param in os.environ.keys():print"%20s:%s<\br>" %(param,os.environ[param])



Simple:Python is a language that represents a simple thought. Reading a good Python program is like reading English. It enables you to focus on solving problems rather than understanding the language itself.

Easy to learn:Python is extremely easy to use, because Python has extremely simple instructions.

Fast:Python is written in C at the underlying layer, and many standard and third-party libraries are written in C at a very fast speed.

Free and open-source:Python is one of FLOSS (Free/open source software. Users can freely publish copies of the software, read its source code, make changes to it, and use part of it in new free software. FLOSS is based on the concept of a group sharing knowledge.

High-level language:When writing a program in Python, you do not need to consider the underlying details such as how to manage the memory used by your program.

Portability:Because of its open-source nature, Python has been transplanted on many platforms (modified to enable it to work on different platforms ). These platforms include Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acom risc OS, VxWorks, PlayStation, Sharp Zaurus, Windows CE, PocketPC, Symbian, and Google android platform developed based on linux.

Description:A program written in a compilation language such as C or C ++ can be converted from the source file (that is, the C or C ++ language) to the language used by your computer (binary code, 0 and 1 ). This process is completed by the compiler and different tags and options.

When running the program, the connection/reprinter software copies your program from the hard disk to the memory and runs it. Programs Written in Python do not need to be compiled into binary code. You can run the program directly from the source code.

Inside the computer, the Python interpreter converts the source code into an intermediate form called bytecode, and then translates it into the machine language used by the computer and runs it. This makes Python easier. It also makes Python programs easier to transplant.

Object-oriented:Python supports both process-oriented and object-oriented programming. In a "process-oriented" language, a program is built by a process or a function that is only reusable code. In the "Object-Oriented" language, a program is built by combining data and functions.

Scalability:If you need a piece of key code to run faster or want some algorithms not to be made public, you can write some programs in C or C ++ and then use them in Python programs.

Embeddable:You can embed Python into C/C ++ programs to provide script functions to program users.

Rich libraries:The Python standard library is indeed huge. It can help with various tasks, including regular expressions, document generation, unit testing, threads, databases, web browsers, CGI, FTP, e-mail, XML, XML-RPC, HTML, WAV Files, password systems, GUI (graphical User Interface), Tk, and other system-related operations. This is called Python's "full functionality" concept. In addition to the standard library, there are many other high-quality libraries, such as wxPython, Twisted, and Python image libraries.

Standard Code: Python uses forced indentation to make the code readable. Programs Written in Python do not need to be compiled into binary code.


Single-line statements and command line output problems: in many cases, you cannot write a program into a single line, such as import sys; for I in sys. path: print I. Perl and awk do not have this restriction, so it is easier to complete simple programs in shell. Like Python, programs must be written into a. py file.

Unique syntax

This may not be called a limitation, but it is still confusing for many beginners to use indentation to distinguish the relationship between statements. Even experienced Python programmers may fall into a trap. The most common case is that the combination of tabs and spaces will lead to errors, which cannot be separated by the naked eye.

Slow running: Compared with C and C ++.


System Programming:The Application Programming Interface (API) is provided to facilitate system maintenance and management. One of the iconic languages in Linux, it is an ideal Programming tool for many system administrators.

Graphic Processing:Supports PIL, Tkinter, and other graphics libraries to facilitate graphic processing.

Mathematical processing:The NumPy extension provides a large number of interfaces with many standard math libraries.

Text processing:The re module provided by python supports regular expressions and the SGML and XML analysis modules. Many programmers use python to develop XML programs.

Database programming:Programmers can communicate with Microsoft SQL Server, Oracle, Sybase, DB2, MySQL, SQLite and other databases by following the Python DB-API (Database Application Programming Interface) specification module. Python comes with a Gadfly module that provides a complete SQL environment.

Network Programming:It provides a wide range of modules supporting sockets programming to facilitate the rapid development of distributed applications. Many large-scale software development plans, such as Zope, Mnet, and BitTorrent. Google, are widely used.

Web programming:The application development language that supports the latest XML technology.

Multimedia Applications:Python's PyOpenGL module encapsulates the "OpenGL Application Programming Interface" for 2D and 3D image processing. The PyGame module can be used to compile game software.

Pymo engine:PYMO is called python memories off. It is an AVG game engine running on Symbian S60V3, Symbian3, S60V5, Symbian3, and Android. It is developed based on the python2.0 platform and is suitable for creating AVG games in the memories off style. Therefore, it is named PYMO.

Hacker programming: python has an hack library with built-in functions that you are familiar with or are not familiar with, but lacks a sense of accomplishment.

Use Python to write simple Crawlers

First, use the urllib2 Module to obtain the corresponding HTML source code.

1. import urllib2 # Call urllib2

2, url = 'HTTP: // Wd = cloga' # assign the url on the right of the equal sign to the url

3, html = urllib2.urlopen (url). read () # The action behind the random name of html is to open the source code page and read

4, print html # print

The url information is stored in the span tag. You can use the regular expression to obtain the url Information.

Development Environment

● IDLE: Python built-in IDE (provided with the python installation package)

● PyCharm: For details, see Baidu Baike PyCharm, developed by the famous JetBrains company, with a complete set of tools to help users improve their efficiency when developing using the Python language, such as debugging, syntax highlighting, Project management, code jump, smart prompts, Automatic completion, unit testing, and version control. In addition, the IDE provides some advanced functions to support professional Web development under the Django framework.

● Komodo and Komodo Edit: the latter is a free Lite version of the former.

● Spyder: Install the advanced IDE provided by Anaconda

● PythonWin: Both ActivePython and pywin32 provide this IDE, which is only applicable to Windows

● SPE (Stani's Python Editor): a free software with many features, based on wxPython

● Ulipad: fully functional free software, based on wxPython; Author: limodou, a Chinese Python expert

● WingIDE: it may be the most functional IDE, but it is not a free software (educational users and open-source users can apply for a free key)

● Eric: PyQt-based free software with powerful functions. Full name: The Eric Python IDE

● DrPython

● PyScripter: a lightweight open-source Python IDE developed by Delphi, which supports Python2.6 and 3.0.

● PyPE: an open-source cross-platform PythonIDE.

● Bpython: a lightweight Python interpreter developed using the curses library in Unix-like operating systems. Syntax prompt function.

● Eclipse + pydev Plugin: facilitates program debugging

● Emacs: comes with python support. functions such as automatic completion and refactor require plug-in support.

● Vim: python support can be added during compilation of the latest version 7.3, providing support for automatic prompt of python code

● Visual Studio 2003 + Visual Python: Only applicable to Windows, with maintenance stopped and poor Functions

● SlickEdit

● Visual Studio 2010 + Python Tools for Visual Studio

● TextMate

● Netbeans IDE

● Sublime

● Ipython

In addition, general programmer text editor software such as Notepad ++, EditPlus, and UltraEdit can also provide support for Python code editing, such as automatic code coloring and comment shortcuts, however, whether it is sufficient to integrate the development environment remains to be evaluated.

The above article is based on the Python _ script CGI, features, applications, and development environment (detailed description). I hope you can give me a reference, we also hope that you can support the customer's home.

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: 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.