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程序(不同页面版本)

相关文章:

  • 2021-06-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-18
  • 2021-04-14
  • 2021-11-08
  • 2021-11-16
  • 2022-02-20
  • 2022-02-27
  • 2021-08-08
相关资源
相似解决方案