Article directory
import the necessary modules
from wsgiref.simple_server import make_server
from utils import load_html, load_template
These two lines of code first import make_server
functions and custom load_html
functions load_template
for subsequent use.
Create routing dictionary
g_url_route = {
}
A global variable is defined g_url_route
as a routing dictionary, which is used to save the correspondence between request paths and processing functions.
decorator functionroute
def route(url):
def handle_action(action):
g_url_route[url] = action
def do_action(start_response):
return action(start_response)
return do_action
return handle_action
route
is a decorator function used when defining routes. It takes one url
parameter and returns an inner function handle_action
. In the inner function, it associates the request path url
with the handler function action
, and defines a new function do_action
that takes start_response
parameters and executes action
the function. Finally return do_action
the function.
Routing definition and processing function
@route('/index.html')
@route('/')
def show_home(start_response):
return load_html('/index.html', start_response)
Here's an example that route
defines two routes using decorators: /index.html
and /
and show_home
associates each with a function. When the request path matches one of these two routes, show_home
the function will be executed.
@route('/test.html')
def show_test(start_response):
start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
return ['我是一段普通的文字'.encode('utf-8')]
Here's another example, using route
a decorator to define a route /test.html
and show_test
associate it with a function. When the request path matches this route, show_test
the function will be executed. This function sets the response header information and returns a piece of ordinary text as the response content.
@route('/info.html')
def show_info(start_response):
return load_template('/info.html', start_response, name='张三', age=18)
A route is also route
defined using a decorator /info.html
and show_info
associated with a function. When the request path matches this route, show_info
the function will be executed. This function uses load_template
the function to load /info.html
the template and passes in the name
and age
parameters for rendering.
request handlerapplication
def application(environ, start_response):
file_name = environ.get('PATH_INFO')
try:
return g_url_route[file_name](start_response)
except Exception:
start_response('404 NOT FOUND', [('Content-Type', 'text/html;charset=utf-8')])
return ['对不起,界面未找到'.encode('utf-8')]
application
A function is the entry point to a WSGI application. It accepts environ
and start_response
two parameters for handling HTTP requests. First, get the request path file_name
. Then, try to find a matching handler function from the routing dictionary, and execute that function. If the corresponding processing function cannot be found, a 404 status code and corresponding error message will be returned.
The server starts and listens
if __name__ == '__main__':
httpd = make_server('', 8000, application)
print("Serving HTTP on port 8000...")
httpd.serve_forever()
This part of the code is used to start the server and listen on the specified port (8000). When the program is run directly (rather than imported as a module), a WSGI server is created and application
functions are called to handle requests. At the same time, the prompt information is printed to indicate that the server is running, and httpd.serve_forever()
continuous monitoring is realized through it.
full code
from wsgiref.simple_server import make_server
from utils import load_html, load_template
g_url_route = {
}
def route(url):
def handle_action(action):
g_url_route[url] = action
def do_action(start_response):
return action(start_response)
return do_action
return handle_action
@route('/index.html')
@route('/')
def show_home(start_response):
return load_html('/index.html', start_response)
@route('/test.html')
def show_test(start_response):
start_response('200 OK', [('Content-Type', "text/html;charset=utf-8")])
return ['我是一段普通的文字'.encode('utf-8')]
@route('/info.html')
def show_info(start_response):
return load_template('/info.html', start_response, name='张三', age=18)
def application(environ, start_response):
file_name = environ.get('PATH_INFO')
try:
return g_url_route[file_name](start_response)
except Exception:
start_response('404 NOT FOUND', [('Content-Type', 'text/html;charset=utf-8')])
return ['对不起,界面未找到'.encode('utf-8')]
if __name__ == '__main__':
httpd = make_server('', 8000, application)
print("Serving HTTP on port 8000...")
httpd.serve_forever()
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model