06 返回静态文件的映射(函数/多线程)web框架

06 返回静态文件的映射(函数/多线程)web框架

服务器serverpython程序(函数版):

 1 import socket
 2 server = socket.socket()
 3 server.bind(("127.0.0.1", 8888))
 4 server.listen()
 5  
 6 def func_html(conn):
 7     with open("index.html", "rb")as f:
 8         conn.send(f.read())
 9  
10  
11 def func_js(conn):
12     with open("index.js", "rb")as f:
13         conn.send(f.read())
14  
15  
16 def func_css(conn):
17     with open("index.css", "rb")as f:
18         conn.send(f.read())
19  
20  
21 def func_img(conn):
22     with open("index.png", "rb")as f:
23         conn.send(f.read())
24  
25  
26 def func_ico(conn):
27     with open("favicon.ico","rb")as f:
28         conn.send(f.read())
29  
30 def respones_back(conn,path):
31     conn.send("HTTP/1.1 200 ok \r\n\r\n".encode("utf-8"))
32  
33     if path == "/":
34         func_html(conn)
35     elif path == "/index.js":
36         func_js(conn)
37     elif path == "/index.css":
38         func_css(conn)
39     elif path == "/index.png":
40         func_img(conn)
41     elif path == "/favicon.ico":
42         func_ico(conn)
43     conn.close()
44     
45 if __name__ == '__main__':    
46     while  1:
47         conn, client_addr = server.accept()
48         http_request=conn.recv(1024).decode("utf-8")
49         path=http_request.split("\r\n")[0].split(" ")[1]
50         print("path>>>",path)
51  
52         respones_back(conn,path)
53  
服务器server端python程序(函数版)

服务器serverpython程序(函数映射版):

  1 import socket
  2 
  3 from threading import Thread,currentThread,activeCount,enumerate
  4 
  5 server = socket.socket()
  6 
  7 server.bind(("127.0.0.1", 8888))
  8 
  9 server.listen()
 10 
 11  
 12 
 13 def func_html(conn):   
 14 
 15     with open("index.html", "rb")as f:
 16 
 17         conn.send(f.read())
 18 
 19  
 20 
 21 def func_js(conn):
 22 
 23     with open("index.js", "rb")as f:
 24 
 25         conn.send(f.read())
 26 
 27  
 28 
 29 def func_css(conn):
 30 
 31     with open("index.css", "rb")as f:
 32 
 33         conn.send(f.read())
 34 
 35  
 36 
 37 def func_img(conn):
 38 
 39     with open("index.png", "rb")as f:
 40 
 41         conn.send(f.read())
 42 
 43  
 44 
 45 def func_ico(conn):
 46 
 47     with open("favicon.ico","rb")as f:
 48 
 49         conn.send(f.read())
 50 
 51  
 52 
 53 def respones_back(conn,path,func_mappers):
 54 
 55     conn.send("HTTP/1.1 200 ok \r\n\r\n".encode("utf-8"))
 56 
 57  
 58 
 59     for mapper in func_mappers:
 60 
 61         if path == mapper[0]:
 62 
 63             mapper[1](conn)
 64 
 65             break
 66 
 67     conn.close()
 68 
 69 #     if path == "/":
 70 
 71 #         func_html(conn)
 72 
 73 #     elif path == "/index.js":
 74 
 75 #         func_js(conn)
 76 
 77 #     elif path == "/index.css":
 78 
 79 #         func_css(conn)
 80 
 81 #     elif path == "/index.png":
 82 
 83 #         func_img(conn)
 84 
 85 #     elif path == "/favicon.ico":
 86 
 87 #         func_ico(conn)
 88 
 89 #     conn.close()
 90 
 91  
 92 
 93 # 将每个请求的路径对应的执行函数进行映射,循环遍历,减少if判断
 94 
 95 func_mappers=[("/",func_html),
 96 
 97          ("/index.js",func_js),
 98 
 99          ("/index.css",func_css),
100 
101          ("/index.png",func_img),
102 
103          ("/favicon.ico",func_ico)]
104 
105  
106 
107 if __name__ == '__main__':
108 
109     while  1:
110 
111         conn, client_addr = server.accept()
112 
113         http_request=conn.recv(1024).decode("utf-8")
114 
115         path=http_request.split("\r\n")[0].split(" ")[1]
116 
117         print("path>>>",path)
118 
119        
120 
121         respones_back(conn,path,func_mappers)
服务器server端python程序(函数映射版)

服务器serverpython程序(多线程版):

  1 import socket
  2 
  3 from threading import Thread,currentThread,activeCount,enumerate
  4 
  5 server = socket.socket()
  6 
  7 server.bind(("127.0.0.1", 8888))
  8 
  9 server.listen()
 10 
 11  
 12 
 13 def func_html(conn):
 14 
 15     print("html>>>",currentThread())
 16 
 17     with open("index.html", "rb")as f:
 18 
 19         conn.send(f.read())
 20 
 21  
 22 
 23 def func_js(conn):
 24 
 25     print("js>>>", currentThread())
 26 
 27     with open("index.js", "rb")as f:
 28 
 29         conn.send(f.read())
 30 
 31  
 32 
 33 def func_css(conn):
 34 
 35     print("css>>>", currentThread())
 36 
 37     with open("index.css", "rb")as f:
 38 
 39         conn.send(f.read())
 40 
 41  
 42 
 43 def func_img(conn):
 44 
 45     print("img>>>", currentThread())
 46 
 47     with open("index.png", "rb")as f:
 48 
 49         conn.send(f.read())
 50 
 51  
 52 
 53 def func_ico(conn):
 54 
 55     print("icon>>>", currentThread())
 56 
 57     with open("favicon.ico","rb")as f:
 58 
 59         conn.send(f.read())
 60 
 61  
 62 
 63 def respones_back(conn,path):
 64 
 65     conn.send("HTTP/1.1 200 ok \r\n\r\n".encode("utf-8"))
 66 
 67     if path == "/":
 68 
 69         func_html(conn)
 70 
 71     elif path == "/index.js":
 72 
 73         func_js(conn)
 74 
 75     elif path == "/index.css":
 76 
 77         func_css(conn)
 78 
 79     elif path == "/index.png":
 80 
 81         func_img(conn)
 82 
 83     elif path == "/favicon.ico":
 84 
 85         func_ico(conn)
 86 
 87     conn.close()
 88 
 89  
 90 
 91 if __name__ == '__main__':
 92 
 93     while  1:
 94 
 95         conn, client_addr = server.accept()
 96 
 97         http_request=conn.recv(1024).decode("utf-8")
 98 
 99         path=http_request.split("\r\n")[0].split(" ")[1]
100 
101         print("path>>>",path)
102 
103        
104 
105         # respone_back(path)
106 
107         #开线程进行异步处理
108 
109         task=Thread(target=respones_back,args=(conn,path))
110 
111         task.start()
112 
113 #查看当前存活的线程和数量
114 
115 
116 
117         # print("threads>>>",activeCount(),enumerate()) 
服务器server端python程序(多线程版)

服务器serverpython程序(多线程映射版):

  1 import socket
  2 
  3 from threading import Thread,currentThread,activeCount,enumerate
  4 
  5 server = socket.socket()
  6 
  7 server.bind(("127.0.0.1", 8888))
  8 
  9 server.listen()
 10 
 11  
 12 
 13 def func_html(conn):
 14 
 15     print("html>>>",currentThread())
 16 
 17     with open("index.html", "rb")as f:
 18 
 19         conn.send(f.read())
 20 
 21  
 22 
 23 def func_js(conn):
 24 
 25     print("js>>>", currentThread())
 26 
 27     with open("index.js", "rb")as f:
 28 
 29         conn.send(f.read())
 30 
 31  
 32 
 33 def func_css(conn):
 34 
 35     print("css>>>", currentThread())
 36 
 37     with open("index.css", "rb")as f:
 38 
 39         conn.send(f.read())
 40 
 41  
 42 
 43 def func_img(conn):
 44 
 45     print("img>>>", currentThread())
 46 
 47     with open("index.png", "rb")as f:
 48 
 49         conn.send(f.read())
 50 
 51  
 52 
 53 def func_ico(conn):
 54 
 55     print("icon>>>", currentThread())
 56 
 57     with open("favicon.ico","rb")as f:
 58 
 59         conn.send(f.read())
 60 
 61  
 62 
 63 def respones_back(conn,path):
 64 
 65     conn.send("HTTP/1.1 200 ok \r\n\r\n".encode("utf-8"))
 66 
 67     for mapper in func_mappers:
 68 
 69         if path == mapper[0]:
 70 
 71             mapper[1](conn)
 72 
 73             break
 74 
 75     conn.close()
 76 
 77     # if path == "/":
 78 
 79     #     func_html(conn)
 80 
 81     # elif path == "/index.js":
 82 
 83     #     func_js(conn)
 84 
 85     # elif path == "/index.css":
 86 
 87     #     func_css(conn)
 88 
 89     # elif path == "/index.png":
 90 
 91     #     func_img(conn)
 92 
 93     # elif path == "/favicon.ico":
 94 
 95     #     func_ico(conn)
 96 
 97     # conn.close()
 98 
 99  
100 
101  
102 
103 # 将每个请求的路径对应的执行函数进行映射,循环遍历,减少if判断
104 
105 func_mappers=[("/",func_html),
106 
107          ("/index.js",func_js),
108 
109          ("/index.css",func_css),
110 
111          ("/index.png",func_img),
112 
113          ("/favicon.ico",func_ico)]
114 
115  
116 
117 if __name__ == '__main__':
118 
119     while  1:
120 
121         conn, client_addr = server.accept()
122 
123         http_request=conn.recv(1024).decode("utf-8")
124 
125         path=http_request.split("\r\n")[0].split(" ")[1]
126 
127         print("path>>>",path)
128 
129  
130 
131         # respone_back(path)
132 
133         #开线程进行异步处理
134 
135         task=Thread(target=respones_back,args=(conn,path))
136 
137         task.start()
138 
139 #查看当前存活的线程和数量
140 
141         # print("threads>>>",activeCount(),enumerate())
服务器server端python程序(多线程映射版) 

index.html


 1 <!DOCTYPEhtml>
 2 
 3 <htmllang="en">
 4 
 5 <head>
 6 
 7 <meta  charset="UTF-8">
 8 
 9 <link  rel="stylesheet"  href="index.css">
10 
11 <link  rel="icon"  href="favicon.ico">
12 
13 <title>映射版</title>
14 
15 </head>
16 
17 <body>
18 
19 <div  id="d1">
20 
21 <h1>映射版的页面:本html页面引用了外部本地css样式和js代码(本地图片)</h1>
22 
23 </div>
24 
25 <img  src="index.png"  alt="本地图片"  title="本地图片">
26 
27 </body>
28 
29 <scriptsrc="index.js"></script>
30 
31 </html>
index.html 


posted @ 2019-07-14 00:22  笑得好美  阅读(174)  评论(0编辑  收藏  举报