-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhenlinWebServerv0.py
More file actions
43 lines (40 loc) · 1.46 KB
/
henlinWebServerv0.py
File metadata and controls
43 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#import socket module
from socket import *
import http.server
import sys # In order to terminate the program
serverPort = 11000
serverSocket = socket(AF_INET, SOCK_STREAM)
#Prepare a server socket
#Fill in start
serverSocket.bind(('192.168.2.55', serverPort))
serverSocket.listen(1)
#Fill in end
while True:
#Establish the connection
print('Ready to serve...')
connectionSocket, addr = serverSocket.accept()
try:
message = connectionSocket.recv(1024).decode()
filename = message.split()[1]
f = open(filename[1:])
outputdata = f.read()
#Send one HTTP header line into socket
#Fill in start
responseMessage = 'HTTP/1.1 200 OK\nContent-Type: text/html'
connectionSocket.send(responseMessage.encode('utf-8'))
#Fill in end
#Send the content of the requested file to the client
for i in range(0, len(outputdata)):
connectionSocket.send(outputdata[i].encode('utf-8'))
connectionSocket.send("\r\n".encode())
connectionSocket.close()
except IOError:
#Send response message for file not found
#Fill in start
#Fill in end
responseMessage = 'HTTP/1.1 404 File not found'
connectionSocket.send(responseMessage.encode('utf-8'))
connectionSocket.close()
#Close client socket
serverSocket.close()
sys.exit()#Terminate the program after sending the corresponding data