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.
- When you forget to bring the U disk, you can simply do a file sharing.
- How to see the system of WiFi transfer book should also be this principle.
- You can also simply test whether the Web page is normal
- 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 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