07 返回多个页面web框架

07 返回多个页面web框架

服务器serverpython程序(不同页面版本):

 1 import socket
 2 
 3 server=socket.socket()
 4 
 5 server.bind(("127.0.0.1",8888))
 6 
 7 server.listen()
 8 
 9  
10 
11 def func_indexHtml(conn):
12 
13     with open("index.html","rb")as f:
14 
15         conn.send(f.read())
16 
17 def func_js(conn):
18 
19     with open("index.js", "rb")as f:
20 
21         conn.send(f.read())
22 
23 def func_css(conn):
24 
25     with open("index.css", "rb")as f:
26 
27         conn.send(f.read())
28 
29 def func_img(conn):
30 
31     with open("index.png", "rb")as f:
32 
33         conn.send(f.read())
34 
35 def func_ico(conn):
36 
37     with open("favicon.ico","rb")as f:
38 
39         conn.send(f.read())
40 
41 def func_html(conn):
42 
43     with open("another.html","rb")as f:
44 
45         conn.send(f.read())
46 
47  
48 
49 def respones_back(conn,path,func_mappers):
50 
51     conn.send(b"HTTP/1.1 200 ok \r\n\r\n")
52 
53     for mapper in func_mappers:
54 
55         if path==mapper[0]:
56 
57             mapper[1](conn)
58 
59             break
60 
61     else:
62 
63         conn.send(b"404 not found!")
64 
65     conn.close()
66 
67  
68 
69 func_mappers=[
70 
71     ("/",func_indexHtml),
72 
73     ("/index.js",func_js),
74 
75      ("/index.css",func_css),
76 
77      ("/index.png",func_img),
78 
79      ("/favicon.ico",func_ico),
80 
81      ("/another.html",func_html)]
82 
83  
84 
85 if __name__ == '__main__':
86 
87     while 1:
88 
89         conn,client_addr=server.accept()
90 
91         http_request=conn.recv(1024).decode("utf-8")
92 
93         path=http_request.split("\r\n")[0].split(" ")[1]
94 
95         print("path>>>",path)
96 
97  
98 
99         respones_back(conn,path,func_mappers)
服务器server端python程序(不同页面版本)

  index.html/another.html

 

 

index.html:

 
 1 <!DOCTYPE html>
 2 
 3 <html lang="en">
 4 
 5 <head>
 6 
 7     <meta charset="UTF-8">
 8 
 9     <meta http-equiv="refresh" content="">
10 
11     <meta name="keywords" content="">
12 
13     <link rel="stylesheet" href="index.css">
14 
15     <link rel="icon" href="favicon.ico">
16 
17     <title>返回不同页面</title>
18 
19 </head>
20 
21 <body>
22 
23 <div id="d1">
24 
25     <h1>返回不同页面页面:本html页面引用了外部本地css样式和js代码(本地图片)</h1>   
26 
27 </div>
28 
29 <span><a href="another.html">点击跳转下一个页面</a></span>
30 
31 <img src="index.png" alt="本地图片" title="本地图片">
32 
33  
34 
35 </body>
36 
37 <script src="index.js"></script>
38 
39 </html>
index.html

another.html

 1 <!DOCTYPE html>
 2 
 3 <html lang="en">
 4 
 5 <head>
 6 
 7     <meta charset="UTF-8">
 8 
 9     <meta http-equiv="refresh" content="">
10 
11     <meta name="keywords" content="">
12 
13     <link rel="stylesheet" href="">
14 
15     <title>跳转的页面</title>
16 
17 </head>
18 
19 <body>
20 
21 <h2>跳转成功!</h2>
22 
23 </body>
24 
25 </html>
another.html
posted @ 2019-07-14 00:27  笑得好美  阅读(267)  评论(0编辑  收藏  举报