in the previous article, "Introduction to Python" Windows 7 under the Python Web development environment build notes, next talk about The specific implementation of the Python language Web service : The first Python Web application-a simple Web server.
Unlike other web back-end languages, the Python language needs to write its own web server.
If you use some existing frameworks, you can omit this step;
If you use Python CGI programming, you can omit this step;
Using Python to build the simplest Web server using Python's own package can build a simple Web server. In DOS CD to prepare to do the server root directory under the path, enter the command:
PYTHON-M Web server module [port number, default 8000]
For example:
Python-m simplehttpserver 8080
Then you can enter it in the browser
http://localhost: Port Number/path
To access server resources.
For example:
Http://localhost:8080/index.htm (of course index.htm files have to be created yourself)
Other machines can also be accessed via the server's IP address.
There are three types of "Web server modules" here:
Basehttpserver: Provides basic Web services and processor classes, respectively, Httpserver and Basehttprequesthandler.
Simplehttpserver: Contains the Simplehttprequesthandler class that performs the get and head requests.
Cgihttpserver: Contains the processing post request and executes the Cgihttprequesthandler class.
Custom handlers
Google provides a service on http://chart.apis.google.com that automatically transforms form data into charts. However, the service is difficult to interact with because you need to put the data in the URL as a query. This program provides a better interface for a data format: given a small piece of text, it will call the chart server to generate a QR code (code), which is a dot matrix of encoded text. The image can be captured by your phone camera and interpreted as a string, such as a URL, which eliminates the hassle of typing a URL on a small phone keypad.
The following is the complete program:
The OS module in the Import Os#python standard library contains common operating system features import re# introduces a regular expression object import urllib# used to encode the URL from the Basehttpserver import Httpserver, Basehttprequesthandler #导入HTTP处理相关的模块 # Custom handler for handling HTTP requests class Testhttphandler (Basehttprequesthandler) : #处理GET请求 def do_get (self): #页面输出模板字符串 templatestr = "Run AccessHere I will save the above code as: C:\Python\webserver\server1.py, using UTF-8 encoding;
Open the command line, switch to the C:\Python\webserver\ directory,
Execution: Python server1.py
A firewall warning may pop up and click Allow access.
Access in the browser, view the effect, open the browser, enter: Http://localhost:8000/, results such as:
Enter a URL in the text box, such as (HTTP://BLOG.CSDN.NET/TESTCS_DN), and click Show QR to generate a picture of a two-dimensional code
Sweep the QR code and take a look.
SummaryUnlike other web back-end languages, the Python language needs to write its own web server;
But Python is a little bit more difficult to get started than the go language.
Here is just a simple example of writing a run to see how the effect looks for the feeling.
Although Python's name has been known for a long time, but the recent real contact, the text of the wrong place, there is a better way to ask seniors to teach!
Reference:
Basic Python Tutorials
Write a WEB server together
Deep understanding of Python WSGI: Write a WEB server together
"Getting Started with Python" The first Python Web program--a simple Web server