File Directory:
dynamic in: Framework
static: css, jss static files
teplates: Templates
web_server.conf: Profile
web_server.py: The main program
run.sh: Run the script
web_server.py:
. 1 Import socket 2 Import multiprocessing . 3 Import Re . 4 Import dynamic.mini_frame . 5 Import SYS . 6 . 7 class WSGIServer (Object): . 8 DEF the __init__ (Self, Port, App, static_path): . 9 # 1. Create a socket object 10 self.tcp_server_socket = socket.socket (socket.AF_INET, socket.SOCK_STREAM) . 11 # 2. set reuse address 12 is self.tcp_server_socket.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR,. 1 ) 13 is # 3 bound port 14 self.tcp_server_socket.bind (( "" , Port)) 15 # 4. Set the listening state 16 self.tcp_server_socket.listen (128 ) . 17 # file application method of a web server template 18 is self.application = App . 19 # config file 20 is self.static_path = static_path 21 is 22 is 23 is 24 DEF clinet_server (Self, new_client_socket): 25 # 1. accept message 26 is Request = new_client_socket.recv (1024) .decode ( " UTF-. 8") 27 lines = request.splitlines() 28 # print("") 29 # print(">" * 20) 30 # print(lines) 31 # 2.匹配请求网页 32 request_name = re.match(r"[^/]+(/[^ ]*)",lines[0]) 33 if request_name: 34 file_name = request_name.group(1) 35 if file_name == "/": 36 file_name = "/index.html" 37 38 # return data to the browser 39 IF Not file_name.endswith ( " .py " ): 40 # 3. Open the file 41 is the try : 42 is F = Open (self.static_path + file_name, " RB " ) 43 is the except Exception AS RET: 44 is Pass 45 the else : 46 is html_content = reached, f.read () 47 f.close () 48 # 4. Create a header and a body 49 response_body = html_content 50 response_header = "HTTP/1.1 200 ok\r\n" 51 response_header += "Content-Length:%d\r\n" % len(response_body) 52 response_header += "\r\n" 53 response = response_header.encode("utf-8") + response_body 54 # 5.发送 55 new_client_socket.send(response) 56 57 58 else: 59 #If is the end .py, it is considered to be a dynamic page request / 60 the env = dict (); 61 is the env [ ' the PATH_INFO ' ] = file_name 62 is Print (the env [ ' the PATH_INFO ' ]) 63 is # file application (dictionary, method name) fixed Usage 64 body = self.application (the env, self.start_respones_header) 65 # assembled header head 66 header = " the HTTP / 1.1% S \ R & lt \ n- " % self.status 67 for TEMP in self.headers: 68 header + = " % S:% S \ R & lt \ n- " % (TEMP [0], TEMP [. 1 ]) 69 header + = " \ R & lt \ n- " 70 # assembled returns data 71 is Response = header + body 72 # Send data 73 is new_client_socket.send (response.encode ( " UTF-. 8 " )) 74 75 # 6. The Close Socket 76 new_client_socket.close () 77 78 DEF start_respones_header (Self, Status, headers): 79 "" "Receiving the stored value application and pass over "" " 80 self.status = Status 81 self.headers = [( " Server " , " mini_web V1.0 " )] 82 self.headers + = headers 83 84 85 DEF run_forever (Self ): 86 "" " run " "" 87 the while True: 88 # 5. receiving the customer's address, create a new socket 89 Since the software of, client_addr = self.tcp_server_socket.accept () 90 # 6. for the new client service 91 multiprocessing.Process = P (= self.clinet_server target, args = (Since the software of,)) 92 p.start () 93 # 7. The closed new client 94 new_socket.close () 95 # 7. The Close Socket 96 self.tcp_server_socket. Close () 97 98 DEF main (): 99 # to transfer the program parameters, sys import library 100 RET = the sys.argv 101 IF len (RET) ==. 3 : 102 # receiving port information 103 port = int (RET [. 1 ] ) 104 # receives the information web server 105 RET = frame_app_name [2 ] 106 the else : 107 Print ( " Please enter the parameter " ) 108 # Web server and the name of the package 109 frame_app = re.match (R & lt " ([^:] +): ([^:] +) " , frame_app_name) 110 frame_name frame_app.group = (. 1 ) 111 APP_NAME = frame_app.group (2 ) 112 113 # imported packages path 114 sys.path.append ( " ./dynamic " ) 115 # import template 1 16 Frame =__import__ (frame_name) 117 # At this point the app dynamic / mini_frame template application function 1 18 app = getattr (Frame, APP_NAME) 119 120 with Open ( " web_server.conf " , " R & lt " ) AS F: 121 # the eval () to { "xx": "xx" } this string into a dictionary 122 config_path = the eval (reached, f.read ()) 123 124 wsgi_server = WSGIServer (Port, App, config_path [ ' static_path ' ]) 125 wsgi_server. run_forever () 126 127 128 IF __name__ == '__main__': 129 main()
mini_frame.py:
1 def center(): 2 with open("./templates/center.html","r") as f: 3 return f.read() 4 5 def index(): 6 with open("./templates/index.html", "r") as f: 7 return f.read() 8 9 def application(env, start_respones): 10 start_respones('200 ok',[("Content-Type","text/html;charset=utf-8")]) 11 file_name = env['PATH_INFO'] 12 # print(file_name) 13 if file_name == "/index.py": 14 return index() 15 elif file_name == "/center.py": 16 return center() 17 else: 18 return "python 中国"
web_server.conf:
{ "static_path":"./static", "dynamic_path":"./dynamic" }
run.sh:
python3 web_server.py 7788 mini_frame:application