Python CGI programming

Source: Internet
Author: User
Tags server port

Python comes with several web-related modules by default. Originally only know simplehttpserver this module, and later know cgihttpserver this module

    • Simplehttpserver
    • 1 python-m  

      At the command line, type the command above to have a simple HTTP server, the server port is 80 (or else, if the 80 port is occupied)

-M means that the root directory of Module,web is the current directory of the command line (Win7 in a folder blank place shift+ the right mouse button will appear here to open the command line, and then enter the above example can be the current directory as the root directory of the web)

Open the browser and enter HTTP://LOCALHOST:80 to see the directories and files under the folder.

    1. When you forget to bring the U disk, you can simply do a file sharing.
    2. How to see the system of WiFi transfer book should also be this principle.
    3. You can also simply test whether the Web page is normal
    4. If the folder has index.html, it will automatically open as the first page
    • Cgihttpserver
    • 1

      The explanation of the command is similar to the above, this time the CGI (Common Gateway Interface) server is launched, and the CGI program can be implemented in any scripting language or fully independent programming language, as long as the language can run on this system.

The CGI server started with the above command, the root of the Web or the current directory of the command line. However, if you put the CGI script directly under the current directory is not executable, you must create a new folder called Cgi-bin, the script is placed in this directory to execute

The popular way is to call a local program through the web, and then output the results to the web, which is an interactive process. CGI Programming for Python can refer to this site: http://www.w3cschool.cc/python/python-cgi.html The above code is not directly run, need to modify

An example is given below:

Origin: The size and rotation of the image of the Amazon product is the URL query inside the parameter control can see this URL http://aaugh.com/imageabuse.html

Now we're going to simulate the size of the parameter.

  

1 #Coding:utf-82 ImportCv,cv23 ImportCGI,CGITB4 5form =CGI. Fieldstorage ()6Size = Form.getvalue ('s')7 8 Print "content-type:image/jpeg\r\n",9 Print    #must have the format of the headerTen  One ifSizeinch['Big','Small']: Aimg = Cv2.imread ('cgi-bin/1.jpg') -     ifSize = ='Big': -resized = Cv2.resize (img, (img.shape[1]*2,img.shape[0]*2)) the     elifSize = ='Small': -resized = Cv2.resize (img, (IMG.SHAPE[1]/2,IMG.SHAPE[0]/2))         -Cv2.imwrite ("cgi-bin/resized.jpg", resized) -f = open ('cgi-bin/resized.jpg','RB') +     PrintF.read (-1) - f.close () + Else: Af = open ('cgi-bin/1.jpg','RB') at     PrintF.read (-1) -F.close ()
View Code

The URL to be accessed should be such that Http://localhost/cgi-bin/img.py?s=small S is a flag of size, there are two parameters for big and small, and by default we return images of the original size. The code is used to OpenCV to resize to twice times or zoom out a few times. Of course, the actual application may be more complex.

The previous section of code is the header of the output HTTP,HTTP header inside the line is \ r \ n header end of the flag is two consecutive \ r \ n

Summarize:

  The above is just a few of the python built-in simple webserver, the function is certainly a little less than the Apache, but occasionally test it can be, do not install such a large software.

If you want to go deep into the Web or find some framework, like Django, tornado, etc.

The simplest, primitive, and straightforward way to develop the web using Python is to use the CGI standard, which is popular in 1998. Now explain how it works from an application perspective: first make a Python script, output the HTML code, and then save the file as a. cgi extension to access the file through the browser. That's it.

Although the implementation is simple, it exposes some problems and inconveniences. Ask yourself these questions:

    • What happens when you have multiple connections to a database in your application? Each stand-alone CGI script should not repeat the code for the database connection. A more practical approach is to write a shared function that can be called by multiple code.

    • Does a developer really need to focus on how to output content-type and to shut down the database after doing all the work? Such problems only reduce the productivity of developers and increase the chances of making mistakes. Those tasks related to initialization and release should be given to some common framework for completion.

    • What happens if such code is reused in a composite environment? Does each page correspond to a separate database and password?

    • What happens if a web designer has no Python experience at all, but needs to redesign the page? One character was incorrectly written, which could cause the entire application to crash. Ideally, the logic of the page display is separated from reading the book records from the database so that the redesign of the Web designer does not affect the previous business logic.

These are the issues that the web framework is dedicated to solving. The web framework provides a set of procedural frameworks for your application, so you can focus on writing clear, maintainable code without having to start from scratch. In simple terms, that's what Django can do.

-------------------The Django book 2.0--Chinese version

  

Python CGI programming

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.