Basic Python and python tutorials

Source: Internet
Author: User

Basic Python and python tutorials
CGI

 

CGI is currently maintained by NCSA. NCSA defines CGI as follows: CGI (Common Gateway Interface) and general Gateway Interface. It is a program that runs on a server such as an HTTP server, provides the same HTML page interface as the 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:
123456789 <Directory "/var/www/cgi-bin"> Allow Override NoneOptions ExecCGIOrder allow, denyAllow from all </Directory> <Directory "/var/www/cgi-bin"> Options All </Directory>
The program uses 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:
12345678910 #! /Usr/bin/env pythonprint ("Content-type: text/html \ 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 ".
All CGI programs of environment variables 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:
123456 #! /Usr/bin/pythonimport osprint "Content-type: text/html \ r \ n" print "Environment" for param in OS. environ. keys (): print"% 20 s: % S <\ br> "% (param, OS. environ [param])
Simple features and advantages: 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. The unique syntax may not be called a limitation, but it uses indentation to differentiate the statement relationship, which still brings confusion to many beginners. 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. To write a simple crawler using Python, you must first obtain the corresponding HTML source code through the Module urllib2.
1234 Import urllib2 # Call urllib2 url = 'HTTP: // www.baidu.com/s? Wd = cloga' # assign the url on the right of the equal sign to urlhtml = urllib2.urlopen (url ). read () # Open the source code page and read print html # print
The above three sentences can be used to store the source code of the URL in the content Variable. the type of the source code is struct. The next step is to extract the content we need from the HTML source code. Use Chrome to view the corresponding Content Code (Firefox Firebug can also be used ). 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 PyCharm in Baidu encyclopedia, developed by the famous JetBrains company, A complete set of tools are provided 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 that comes with Anaconda ● PythonWin: ActivePython or pywin32 provides this IDE, only applicable to Windows ● SPE (Stani's Python Editor): a free software with many features, based on wxPython ● Ulipad: a 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 free software (educational users and open-source users can apply for free keys) ● Eric: PyQt-based free software, powerful functions. The full name is: The Eric Python IDE ● DrPython ● PyScripter: a lightweight open-source Python IDE developed using 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 plug-in: easy to debug the program ● emacs: comes with python support, Automatic completion, refactor, and other functions require plug-in support ● Vim: python support can be added to the latest version 7.3 during compilation, support for python code automatic prompts ● Visual Studio 2003 + Visual python: Only applicable to Windows, maintenance has been stopped, poor functionality ● 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 shortcut keys, however, whether it is sufficient to integrate the development environment remains to be evaluated.

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.