用python搭建最简单的服务器与网站
1、首先我们要先利用python的wsgiref库里的simple_server搭建一个基础的服务器。
代码如下:
# server.py
# 从wsgiref模块导入:
from wsgiref.simple_server import make_server
# 导入我们自己编写的application函数:
from test import application
# 创建一个服务器,IP地址为空,端口是8000,处理函数是application:
httpd = make_server('', 8000, application)
print("Serving HTTP on port 8000...")
# 开始监听HTTP请求:
httpd.serve_forever()

2、在命令提示行中运行server.py,出现如图所示就表示我们搭建成功

3、我们要有一个python文件对网页进行解析并输送到前端
代码如下:
import re
import os
import codecs
def application(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
f = open("D:\\web\\text.html","r",encoding="utf-8")
b = f.read()
body = re.sub("{tittle}",'python Web',b)
body1 = re.sub("{content}",'你好 pyweb!',body)
f.close()
return [body1.encode()]

4、再来一个简单的静态网页
代码如下
<!DOCTYPE html>
<html>
<head>
<title>{tittle}</title>
</head>
<body>
<a href="">{content}</a>
</body>
</html>

5、看一下效果,大功告成
