@@ -24,37 +24,30 @@ def handle_client(conn, addr):
24
24
global message
25
25
26
26
print (f"[NEW CONNECTION] { addr } connected." )
27
- connected = True
28
- while connected :
29
- try :
30
- message = conn .recv (1024 ).decode ()
31
- filename = message .split ()[1 ]
32
- f = open (filename [1 :], 'r' )
33
- outputdata = f .read ()
34
- # Send one HTTP header line into socket
35
- message = conn .recvfrom (1024 ).decode ()
36
- conn .send ("HTTP/1.1 200 OK\r \n \r \n " .encode ())
37
- conn .send ("\r \n " .encode ())
38
- # Send the content of the requested file to the client
39
- for i in range (0 , len (outputdata )):
40
- conn .send (outputdata [i ].encode ())
41
- conn .send ("\r \n " .encode ())
42
- # Terminate connection
43
- conn .close ()
44
-
45
- except IOError :
46
- # Send response message for file not found
47
- conn .send ("HTTP/1.1 404 Not Found\r \n \r \n " .encode ())
48
- conn .send ("<html><head>404 Not Found</head><body></body></html>\r \n " .encode ())
49
- conn .close ()
50
-
51
- # Close client socket
52
- # Fill in start
27
+ try :
28
+ message = conn .recv (1024 ).decode ()
29
+ filename = message .split ()[1 ]
30
+ f = open (filename [1 :], 'r' )
31
+ outputdata = f .read ()
32
+
33
+ # Send one HTTP header line into socket
34
+ conn .send ("HTTP/1.1 200 OK\r \n \r \n " .encode ())
35
+ conn .send ("\r \n " .encode ())
36
+ # Send the content of the requested file to the client
37
+ for i in range (0 , len (outputdata )):
38
+ conn .send (outputdata [i ].encode ())
39
+ conn .send ("\r \n " .encode ())
40
+
41
+ except IOError :
42
+ # Send response message for file not found
43
+ conn .send ("HTTP/1.1 404 Not Found\r \n \r \n " .encode ())
44
+ conn .send ("<html><title>404 Not Found</title><body><h1>404 Not Found</h1></body></html>\r \n " .encode ())
53
45
conn .close ()
54
- # Fill in end
55
- serverSocket .close ()
46
+
47
+ # Close client socket
48
+ conn .close ()
56
49
57
50
58
51
print ("[STARTING] server is starting..." )
59
52
start ()
60
- sys .exit () # Terminate the program after sending the corresponding data
53
+ sys .exit () # Terminate the program
0 commit comments