Basic Python and python tutorials
CGI
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.
Server
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:
|
/1. <Directory "/var/www/cgi-bin">/2. Allow Override None /3. Options ExecCGI /4. Order allow, deny /5, Allow from all/6, </Directory>/7, <Directory "/var/www/cgi-bin">/8, Options All/9, </Directory> |
Program
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:
|
1 ,#! /Usr/bin/env python 2. print ("Content-type: text/html \ r \ n ") 3. print ("4. print ("7. print ("<body> ") 8. print ("9. print ("</body> ") 10. 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:
1 ,#! /Usr/bin/python2, import os3, print "Content-type: text/html \ r \ n" 4, print "Environment" 5, for param in OS. environ. keys (): 6, print"% 20 s: % S <\ br> "% (param, OS. environ [param]) |
Features and advantages
Simple: Python is a language that represents the simple ideology. 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 language at the underlying layer. Many standard and third-party libraries are also written in C and run very fast.
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 (changed 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 compiled language such as C or C ++ can be written from the source file (C or C ++) convert to a language used by your computer (binary code, namely, 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.
Embedding: 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.
Disadvantages
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 ++.
Application
System Programming: Provides Application Programming Interface (Application Programming Interface) to facilitate system maintenance and management. One of the iconic languages in Linux is an ideal Programming tool for many system administrators.
Graph processing: supports PIL, Tkinter, and other graphics libraries to facilitate graph processing.
Mathematical processing: The NumPy extension provides a large number of interfaces with many standard mathematical 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: provides a wide range of modules to support sockets programming, allowing you to easily and quickly develop 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 "OpenGL application programming interfaces" for 2D and 3D image processing. The PyGame module can be used to compile game software.
Pymo engine: called python memories off, PYMO is an AVG game engine running on Symbian S60V3, Symbian3, S60V5, Symbian3, and Android systems. 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: // www.baidu.com/s? 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.
If you want to know more about the technical issues of Python, follow these steps.
For more information, see:Mingli.com
A friend needs to add a ball :,
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.