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程序(函数版)

相关文章:

  • 2021-08-10
  • 2021-12-28
  • 2021-10-19
  • 2022-01-31
  • 2021-11-09
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-27
  • 2021-08-22
  • 2021-11-18
  • 2021-11-13
  • 2021-09-23
  • 2022-01-18
相关资源
相似解决方案