from wsgiref.simple_server import make_server #wsgiref是服务器类似于njax
#调用服务器模块下的make_server
#这只是模拟服务器,以后肯定是njax或者是apache
import time
def fool(req):
f=open("abc.html","rb")
data=f.read()
return data
def fool2(req):
f = open("abc.html", "rb")
data = f.read()
return data
def login(req):
print(req["QUERY_STRING"])#测试输出,找到键值对的详细信息发现query保存user和pwd
return b"welcome"
def signup(req):
pass
def show_time(req):
times=time.ctime()#数值
# return ("<h1>time:%s</h1>"%str(times)).encode('utf8')
f=open("show_time.html","rb")
data=f.read()
data=data.decode("utf8")
data=data.replace("{{time}}",str(times))
return data.encode("utf8")
def router():
url_patterns=[
("/login",login),
("/signup",signup),
("/tom",fool),
("/alex",fool2),
("/show_time",show_time),
]
return url_patterns
#定义函数,参数
def application(environ, start_response):
#print("path" ,environ["PATH_INFO"])
start_response('200 OK', [('Content-Type', 'text/html')])
path=environ["PATH_INFO"]
urlit=router()
func=None
for item in urlit:
if item[0]==path:#/tom匹配成功
func=item[1]#对应的函数付给它,fool()
break#匹配没成功停止
if func:
return [func(environ)]#执行函数
if not func:
return [b"error"]
httpd = make_server('', 8080, application)
print('Serving HTTP on port 8080...')
# 开始监听HTTP请求:
httpd.serve_forever()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="jquery-3.3.1.min.js"></script>
<body>
<h1>时间:{{time}}</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://127.0.0.1:8080/login" method="get">
<p>用户名:<input type="text" name="user"></p>
<p>密码:<input type="text" name="pwd"></p>
<p><input type="submit">提交</p>
</form>
</body>
</html>